码迷,mamicode.com
首页 > 编程语言 > 详细

python __str__ 与 __repr__区别

时间:2018-02-28 17:31:47      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:init   用户交互   cep   test   exception   return   span   __init__   nbsp   

>>> class test():
    def __init__(self):
        pass
    def __repr__(self):
        return 1sdf

>>> t = test() 
>>> t #不用print 直接打印出重构的值,面向程序,在程序中传递
1sdf
>>> class test(Exception):
    def __init__(self):
        pass
    def __str__(self): #p
        return 1sdf
  
>>> print(test()) #调用print打印重构值, 面向用户交互,首先调用__str__的值
1sdf
>>> test() #打印输出为函数

 

 

python __str__ 与 __repr__区别

标签:init   用户交互   cep   test   exception   return   span   __init__   nbsp   

原文地址:https://www.cnblogs.com/alplf123/p/8484477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!