引言
类的 __repr__() 方法定义了实例化对象的输出信息,重写该方法,可以输出我们想要的信息,对类的实例化对象有更好的了解。
描述
object.__repr__(self)
Called by the repr() built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <…some useful description…> should be returned. The return value must be a string object. If a class defines
__repr__()but not__str__(), then__repr__()is also used when an “informal” string representation of instances of that class is required.
由

本文介绍了如何通过重写Python类的__repr__()方法,自定义实例化对象的输出信息,提升对象描述的可读性和理解性。通过实例和参考链接详细展示了其实现过程和用法。

1268

被折叠的 条评论
为什么被折叠?



