[Overview of Artificial Intelligence] Python’s wonderful use of __str__()

[Overview of Artificial Intelligence] Python’s wonderful use of str ()


1.Python built-in functions__str__()

  • Related content in the object can be printed by customizing the __str__() function.
class Person(object):
    def __init__(self, name = 'tom', age = 10):
        self.name = name
        self.age = age
        self.parent = None
    
    def __str__(self):
        return "Your name is: " + self.name
    
tom = Person()
print(tom)
 
# 唔,果然输出了我们自定义的内容
# >>> Your name is: tom

Guess you like

Origin blog.csdn.net/qq_44928822/article/details/131733943