TypeError:“字节”对象不可调用

Ray*_*Ray 2 python byte

编辑:

bytes在执行以下代码之前,我犯了一个非常琐碎的错误,即绑定到其他东西。现在,这个问题非常琐碎,可能对任何人都没有帮助。抱歉。

原始问题:

码:

import sys
print(sys.version)
b = bytes([10, 20, 30, 40])
print(b)
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)

说明文件:

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
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Mar*_*ers 5

您已为名称分配了一个bytesbytes

>>> 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
Run Code Online (Sandbox Code Playgroud)

bytes现在绑定到value b'\n\x14\x1e(',该值不可调用。这种全局性正在掩盖内置函数。删除它:

del bytes
Run Code Online (Sandbox Code Playgroud)

再次显示内置的。

  • @Ray在1999年,洛克希德·马丁公司的某人将一架价值1.25亿美元的航天器撞向火星,因为他们使用的是英制单位而不是公制单位。不要为此感到难过! (3认同)
  • 哦,哇,这是我所做的最愚蠢的事情。请给我更多的票。 (2认同)