Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TypeError: 'bytes' object is not callable error while trying to converting to bytes.

4,564 views
Skip to first unread message

Satish ML

unread,
Aug 5, 2014, 1:47:51 AM8/5/14
to
Hi,


>>>import struct
>>>file = open('data.bin', 'rb')
>>>bytes = file.read()
>>> records = [bytes([char] * 8) for char in b'spam']
Traceback (most recent call last):
File "<pyshell#99>", line 1, in <module>
records = [bytes([char] * 8) for char in b'spam']
File "<pyshell#99>", line 1, in <listcomp>
records = [bytes([char] * 8) for char in b'spam']
TypeError: 'bytes' object is not callable


If we code something like given below, it works.

>>> records = [([char] * 8) for char in b'spam']
>>> records
[[115, 115, 115, 115, 115, 115, 115, 115], [112, 112, 112, 112, 112, 112, 112, 112], [97, 97, 97, 97, 97, 97, 97, 97], [109, 109, 109, 109, 109, 109, 109, 109]]

Could you kindly help me resolve this problem of converting to bytes?

Ben Finney

unread,
Aug 5, 2014, 1:57:13 AM8/5/14
to pytho...@python.org
Satish ML <satishm...@gmail.com> writes:

> >>>import struct
> >>>file = open('data.bin', 'rb')

Here you re-bind the name ‘file’ to the return value from that call.

> >>>bytes = file.read()

Here you re-bind the name ‘bytes’ to the return value from that call.

> >>> records = [bytes([char] * 8) for char in b'spam']

Here you attempt to call ‘bytes’, which (as the error says) is not
callable.

You should choose names which are not already bound::

in_file = open('data.bin', 'rb')
in_file_content = in_file.read()
records = [bytes([char] * 8) for char in in_file_content]

When choosing names, try to communicate the *purpose* of the value, its
semantic meaning. The type should be of secondary importance, and almost
always should not be part of the name.

--
\ “Institutions will try to preserve the problem to which they |
`\ are the solution.” —Clay Shirky, 2012 |
_o__) |
Ben Finney

Chris Angelico

unread,
Aug 5, 2014, 1:57:32 AM8/5/14
to pytho...@python.org
On Tue, Aug 5, 2014 at 3:47 PM, Satish ML <satishm...@gmail.com> wrote:
>>>>bytes = file.read()

You've just shadowed the built-in type 'bytes' with your own 'bytes'.
Pick a different name for this, and you'll be fine. 'data' would work.

ChrisA

Travis Griggs

unread,
Aug 6, 2014, 1:31:58 AM8/6/14
to Chris Angelico, pytho...@python.org
Until python4 introduces the 'data' built in. :)

Chris Angelico

unread,
Aug 6, 2014, 1:37:10 AM8/6/14
to pytho...@python.org
Python 3.6 could introduce that, no need to wait for Python 4. :)
However, it wouldn't be critical to this code, unless the builtin's
meaning is also wanted; it'd be on par with the shadowing of 'file'
earlier - given how rarely Python programs actually need 'file'
(rather than 'open'), it's not a big deal to shadow that one.

ChrisA

Wasia Maya

unread,
Dec 4, 2021, 3:41:01 PM12/4/21
to
You have assigned a bytes value to the name 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 is now bound to the value b'\n\x14\x1e(', which is not callable. This global is shadowing the built-in. Delete it:

del bytes
0 new messages