[英]How to fix 'RecursionError: maximum recursion depth exceeded' when calling object?
[英]Maximum recursion depth exceeded when using a numpy.bytes_ object in string formatting
代碼應該說明一切:
$ python
Python 3.3.0 (default, Dec 22 2012, 21:02:07)
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> '{}'.format(np.bytes_(b'Hello'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: maximum recursion depth exceeded while calling a Python object
>>> np.version.version
'1.7.0'
str
和repr
都在np.bytes_(b'Hello')
上返回"b'Hello'"
,我可以print(np.bytes_(b'Hello'))
就好了,但在格式字符串中它會歸入遞歸環。
我是愚蠢的還是它看起來確實是什么,即numpy
的問題? 即使是這樣,我也不太明白發生了什么。 有人可以解釋一下嗎?
我沒有用Python 2重現它。
{}
的行為是調用np.bytes_(b'Hello').__format__()
。 看來__format__
正在調用自己的bug。 看到這張相關的票
這是一個解決方法。
Python 3.2.3 (default, Oct 19 2012, 19:53:57)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> '{}'.format(np.bytes_(b'Hello'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: maximum recursion depth exceeded while calling a Python object
>>> '{!s}'.format(np.bytes_(b'Hello'))
"b'Hello'"
>>> '{!r}'.format(np.bytes_(b'Hello'))
"b'Hello'"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.