Python:__repr__的一个解决办法

1._repr_方法默认返回该对象实现类的“类名+object at +内存地址”值
示例:

<__main__.Student object at 0x000001BD48814148>

2.在定义的类中,若加入:

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

示例对应变为:

(hill, 60)

猜你喜欢

转载自blog.csdn.net/qq_40797015/article/details/111998336