Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #75717 > unrolled thread
| Started by | Satish ML <satishmlwizpro@gmail.com> |
|---|---|
| First post | 2014-08-04 22:47 -0700 |
| Last post | 2014-08-06 15:37 +1000 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
TypeError: 'bytes' object is not callable error while trying to converting to bytes. Satish ML <satishmlwizpro@gmail.com> - 2014-08-04 22:47 -0700
Re: TypeError: 'bytes' object is not callable error while trying to converting to bytes. Ben Finney <ben+python@benfinney.id.au> - 2014-08-05 15:57 +1000
Re: TypeError: 'bytes' object is not callable error while trying to converting to bytes. Chris Angelico <rosuav@gmail.com> - 2014-08-05 15:57 +1000
Re: TypeError: 'bytes' object is not callable error while trying to converting to bytes. Travis Griggs <travisgriggs@gmail.com> - 2014-08-05 22:31 -0700
Re: TypeError: 'bytes' object is not callable error while trying to converting to bytes. Chris Angelico <rosuav@gmail.com> - 2014-08-06 15:37 +1000
| From | Satish ML <satishmlwizpro@gmail.com> |
|---|---|
| Date | 2014-08-04 22:47 -0700 |
| Subject | TypeError: 'bytes' object is not callable error while trying to converting to bytes. |
| Message-ID | <02baad14-39b1-41a0-a47d-28772c97ea54@googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2014-08-05 15:57 +1000 |
| Message-ID | <mailman.12659.1407218248.18130.python-list@python.org> |
| In reply to | #75717 |
Satish ML <satishmlwizpro@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
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-08-05 15:57 +1000 |
| Message-ID | <mailman.12660.1407218261.18130.python-list@python.org> |
| In reply to | #75717 |
On Tue, Aug 5, 2014 at 3:47 PM, Satish ML <satishmlwizpro@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
[toc] | [prev] | [next] | [standalone]
| From | Travis Griggs <travisgriggs@gmail.com> |
|---|---|
| Date | 2014-08-05 22:31 -0700 |
| Message-ID | <mailman.12686.1407303124.18130.python-list@python.org> |
| In reply to | #75717 |
> On Aug 4, 2014, at 22:57, Chris Angelico <rosuav@gmail.com> wrote: > > On Tue, Aug 5, 2014 at 3:47 PM, Satish ML <satishmlwizpro@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. Until python4 introduces the 'data' built in. :)
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-08-06 15:37 +1000 |
| Message-ID | <mailman.12687.1407303433.18130.python-list@python.org> |
| In reply to | #75717 |
On Wed, Aug 6, 2014 at 3:31 PM, Travis Griggs <travisgriggs@gmail.com> wrote: >> On Aug 4, 2014, at 22:57, Chris Angelico <rosuav@gmail.com> wrote: >> >> On Tue, Aug 5, 2014 at 3:47 PM, Satish ML <satishmlwizpro@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. > > Until python4 introduces the 'data' built in. :) 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web