3

代码应该不言自明:

$ 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'

strreprreturn "b'Hello'"on np.bytes_(b'Hello'),我都可以,print(np.bytes_(b'Hello'))但是在格式字符串中它会陷入递归循环。

我是愚蠢还是它确实看起来是这样,即存在问题numpy?即使是这样,我也不太明白发生了什么。有人可以解释一下吗?

我没有用 Python 2 复制它。

4

1 回答 1

2

的行为{}是调用np.bytes_(b'Hello').__format__(). 似乎有一个错误__format__正在调用自己。查看此相关票证

这是一种解决方法。

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'"
于 2013-03-17T22:19:55.563 回答