类型错误:'bytes'对象不可调用。

3

编辑:

在执行以下代码之前,我犯了一个非常微不足道的错误,将bytes绑定到先前的其他内容上。这个问题现在完全是微不足道的,可能对任何人都没有帮助。抱歉。

原始问题:

代码:

import sys
print(sys.version)
b = bytes([10, 20, 30, 40])
print(b)

输出:

3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-38-21fec5626bc3> in <module>()
      1 import sys
      2 print(sys.version)
----> 3 b = bytes([10, 20, 30, 40])
      4 print(b)

TypeError: 'bytes' object is not callable

文档:

Type:        bytes
String form: b'hello world'
Length:      11
Docstring:
bytes(iterable_of_ints) -> bytes
bytes(string, encoding[, errors]) -> bytes
bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
bytes(int) -> bytes object of size given by the parameter initialized with null bytes
bytes() -> empty bytes object

Construct an immutable array of bytes from:
  - an iterable yielding integers in range(256)
  - a text string encoded using the specified encoding
  - any object implementing the buffer API.
  - an integer

我做错了什么?

这对我很有帮助。我也做了同样的事情。 :) - skelliam
1个回答

5
您已将一个bytes值分配给名称bytes:
>>> bytes([10, 20, 30, 40])
b'\n\x14\x1e('
>>> bytes = bytes([10, 20, 30, 40])
>>> bytes([10, 20, 30, 40])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'bytes' object is not callable

bytes现在绑定到值b'\n\x14\x1e(',这是一个不可调用的值。这个全局变量掩盖了内置变量。请将其删除:

del bytes

重新显示内置内容。

2
哦,哇,那是我做过的最愚蠢的事情。请再给我点踩吧。 - Ray
3
1999年,洛克希德·马丁公司的一位人员由于使用英制单位而将价值1.25亿美元的宇宙飞船坠毁在火星上。不要太为此感到难过! - user1717828

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接