首页 > 其他 > 详细

__str__ and __repr__

时间:2021-09-09 04:13:05      阅读:32      评论:0      收藏:0      [点我收藏+]

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:

  • print()
  • str()

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).

__str__ and __repr__

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!