TypeError:' bytes'对象不可调用

时间:2016-09-02 12:04:05

标签: python byte

编辑:

我犯了一个非常微不足道的错误,即在执行以下代码之前将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

我做错了什么?

1 个答案:

答案 0 :(得分:4)

您已将 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

再次显示内置。