__str__ and __repr__

时间:2021-09-09 04:13:05   收藏:0   阅读:31

The __str__ method in Python represents the class objects as a string – it can be used for classes. The __str__ method should be defined in a way that is easy to read and outputs all the members of the class. This method is also used as a debugging tool when the members of a class need to be checked.

The __str__ method is called when the following functions are invoked on the object and return a string:

If we have not defined the __str__, then it will call the __repr__ method. The __repr__ method returns a string that describes the pointer of the object by default (if the programmer does not define it).

原文:https://www.cnblogs.com/shamoguzhou/p/15239988.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!