object.__str__(self) Called by the str() built-in function and by the print statement to compute the “informal” string representation of an object. This differs from __repr__() in that it does not have to be a valid Python expression: a more convenie…
For ( __str__ ),we going to see a example ... and find who is working for ... #!/usr/bin/python class Person(object): def __init__(self,name,gender): self.name = name self.gender = gender if __name__ == '__main__': p = Person('Frank',23) print p See,…
class Cat: def __init__(self,_name): self.name = _name def __str__(self): return "i am %s"%self.name def show(self): print("name is %s"%self.name) tom = Cat("tom") tom.show() print(tom) print("------------"); lanmao…
看下面的例子就明白了 class Test(object): def __init__(self, value='hello, world!'): self.data = value >>> t = Test() >>> t <__main__.Test at 0x7fa91c307190> >>> print t <__main__.Test object at 0x7fa91c307190> # 看到了么?上面打印类对象并不…