Python: a solution for __repr__

1. The _repr_ method returns the "class name + object at + memory address" value of the object implementation class by default.
Example:

<__main__.Student object at 0x000001BD48814148>

2. In the defined class, if you add:

def __str__(self):
  return '(%s, %d)' % (self.name, self.grade)
__repr__ = __str__  

The example becomes:

(hill, 60)

Guess you like

Origin blog.csdn.net/qq_40797015/article/details/111998336