鱼C论坛

 找回密码
 立即注册
查看: 3547|回复: 4

[已解决]__str__ 和__repr__的区别?

[复制链接]
发表于 2016-2-2 10:08:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
小甲鱼在视频里举了两个例子
  1. >>> class A():
  2.         def __str__(self):
  3.                 return "Aloha!"

  4.        
  5. >>> a = A()
  6. >>> a
  7. <__main__.A object at 0x0000000003BA8588>
  8. >>> print(a)
  9. Aloha!
复制代码

  1. >>> class A():
  2.         def __repr__(self):
  3.                 return "Hello!"

  4. >>> a = A()
  5. >>> a
  6. Hello!
复制代码


但我还是不太明白是怎么用的,这两个又有什么区别,在什么时候调用这两个函数?
最佳答案
2016-2-2 11:36:50
shuofxz 发表于 2016-2-2 10:54
这个啥也没写呀

都告诉你了是定义对象调用什么方法了, 查手册呀
object.__str__(self)
Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object.

This method differs from object.__repr__() in that there is no expectation that __str__() return a valid Python expression: a more convenient or concise representation can be used.

The default implementation defined by the built-in type object calls object.__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.

This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-2-2 10:45:57 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-2-2 10:54:43 | 显示全部楼层
hldh214 发表于 2016-2-2 10:45
http://bbs.fishc.com/forum.php?mod=viewthread&tid=48793

搜狗截图20160202105047.png

这个啥也没写呀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-2-2 11:36:50 | 显示全部楼层    本楼为最佳答案   
shuofxz 发表于 2016-2-2 10:54
这个啥也没写呀

都告诉你了是定义对象调用什么方法了, 查手册呀
object.__str__(self)
Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object.

This method differs from object.__repr__() in that there is no expectation that __str__() return a valid Python expression: a more convenient or concise representation can be used.

The default implementation defined by the built-in type object calls object.__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.

This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-2-2 17:40:31 | 显示全部楼层
这里面还是没有说明问题呀
为什么第一个例子要print(a) 才能显示return的字符串
第二个例子只用 a 就能显示return的字符串
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-19 12:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表