鱼C论坛

 找回密码
 立即注册
查看: 1263|回复: 3

[已解决]为什么出现了错误

[复制链接]
发表于 2020-8-15 15:22:02 | 显示全部楼层 |阅读模式

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

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

x
  1. class Nstr(str):
  2.         def __str__(self):
  3.                 print('str方法被调用了!')
  4.                 return '__str__:{}'.format(self)
  5.         def __repr__(self):
  6.                 print('repr方法被调用了!')
  7.                 return '__repr__:{}'.format(self)

  8. >>>s = Nstr('hello')
  9. repr方法被调用了!
  10. str方法被调用了!
  11. str方法被调用了!
  12. str方法被调用了!
  13. ...
  14. Traceback (most recent call last):
  15.   File "<stdin>", line 1, in <module>
  16.   File "<stdin>", line 7, in __repr__
  17.   File "<stdin>", line 4, in __str__
  18.   File "<stdin>", line 4, in __str__
  19.   File "<stdin>", line 4, in __str__
  20.   [Previous line repeated 245 more times]
  21.   File "<stdin>", line 3, in __str__
  22. RecursionError: maximum recursion depth exceeded while calling a Python object
复制代码

为什么会出现递归错误?
最佳答案
2020-8-15 15:25:09
因为你 '__repr__:{}'.format(self) 会调用 str(self),而 __str__ 方法中的 '__str__:{}'.format(self) 又会调用 str(self),无限递归所以报错了。

这样即可:

  1. class Nstr(str):
  2.         def __str__(self):
  3.                 print('str方法被调用了!')
  4.                 return '__str__'
  5.         def __repr__(self):
  6.                 print('repr方法被调用了!')
  7.                 return '__repr__'
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-15 15:25:09 | 显示全部楼层    本楼为最佳答案   
因为你 '__repr__:{}'.format(self) 会调用 str(self),而 __str__ 方法中的 '__str__:{}'.format(self) 又会调用 str(self),无限递归所以报错了。

这样即可:

  1. class Nstr(str):
  2.         def __str__(self):
  3.                 print('str方法被调用了!')
  4.                 return '__str__'
  5.         def __repr__(self):
  6.                 print('repr方法被调用了!')
  7.                 return '__repr__'
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-15 15:47:27 | 显示全部楼层
zltzlt 发表于 2020-8-15 15:25
因为你 '__repr__:{}'.format(self) 会调用 str(self),而 __str__ 方法中的 '__str__:{}'.format(self) 又 ...

我更改了代码,新的问题又出现了:

  1. class Nstr(str):
  2.         def __str__(self):
  3.                 print('str方法被调用了!')
  4.                 return 'str'
  5.         def __repr__(self):
  6.                 print('repr方法被调用了!')
  7.                 return 'repr'

  8. >>>a = Nstr('hello')
  9. str方法被调用了!
  10. str方法被调用了!
  11. >>>a
  12. repr方法被调用了!
  13. repr
  14. repr方法被调用了!
  15. str方法被调用了!
  16. repr方法被调用了!
  17. str方法被调用了!
复制代码

为什么两个方法反复被调用了多次,但返回的次数却那么少呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-15 15:58:08 | 显示全部楼层
鱼cpython学习者 发表于 2020-8-15 15:47
我更改了代码,新的问题又出现了:

为什么两个方法反复被调用了多次,但返回的次数却那么少呢?

建议换个 Python
  1. >>> class Nstr(str):
  2.         def __str__(self):
  3.                 print('str方法被调用了!')
  4.                 return 'str'
  5.         def __repr__(self):
  6.                 print('repr方法被调用了!')
  7.                 return 'repr'

  8.         
  9. >>> a = Nstr('hello')
  10. >>> a
  11. repr方法被调用了!
  12. repr
  13. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-2 19:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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