1. 程式人生 > >python __str__ 與 __repr__區別

python __str__ 與 __repr__區別

cep test style clas spa __init__ str exce n)

>>> 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__區別