Magic method "five" __repr__

_repr__ and __str__ are two methods for displaying the
__str__ is user-oriented, the value is converted to a string suitable for human reading
__repr__ oriented programmers, the value is read into interpreter for take the form of a string

class House(object):

    def __init__(self, new_name, new_age):
        self.name = new_name
        self.age = new_age

    def __str__(self):
         return "%s的年龄%d" % (self.name, self.age)

    def __repr__(self):
        return "%s的年龄%d" % (self.name, self.age)


chitu = House('chitu', 30)


print(chitu)
He published 196 original articles · won praise 34 · views 120 000 +

Guess you like

Origin blog.csdn.net/a6864657/article/details/103942398