__str__
如果一个类中定义了__str__方法,那么打印对象时,默认输出该方法的返回值
class Foo(object):
def __str__(self):
return "laowang"
obj = Foo()
print(obj) # laowang
如果一个类中定义了__str__方法,那么打印对象时,默认输出该方法的返回值
class Foo(object):
def __str__(self):
return "laowang"
obj = Foo()
print(obj) # laowang
本文标题:Python魔法方法-__str__
本文链接:https://www.haomeiwen.com/subject/kkbafhtx.html
网友评论