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

Ben Finney ben+python at benfinney.id.au
Tue Aug 5 01:57:13 EDT 2014


Satish ML <satishmlwizpro at 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




More information about the Python-list mailing list