Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5604

Re: Python 3.x and bytes

Date 2011-05-17 15:50 -0400
From Corey Richardson <kb1pkl@aim.com>
Subject Re: Python 3.x and bytes
References <4DD2C2A5.3080403@stoneleaf.us>
Newsgroups comp.lang.python
Message-ID <mailman.1703.1305661863.9059.python-list@python.org> (permalink)

Show all headers | View raw


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 05/17/2011 02:47 PM, Ethan Furman wrote:
> In Python 3 one can say
> 
> --> huh = bytes(5)
> 
> Since the bytes type is actually a list of integers, I would have 
> expected this to have huh being a bytestring with one element -- the 
> integer 5.  Actually, what you get is:
> 
> --> huh
> b'\x00\x00\x00\x00\x00'
> 
> or five null bytes.  Note that this is an immutable type, so you cannot 
> go in later and say

For the bytes to actually be a 'list of integers', you need to pass it
an iterable, ex:
>>> bytes([5, 6, 1, 3])
b'\x05\x06\x01\x03'

- From help(bytes):
 |  bytes(iterable_of_ints) -> bytes
 |  bytes(string, encoding[, errors]) -> bytes
 |  bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer
 |  bytes(memory_view) -> bytes

Looks like you're using the fourth when you want the first, possibly?

- -- 
Corey Richardson
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.17 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJN0tF1AAoJEAFAbo/KNFvp41AH/1l2zR6XVOJ0xM7s2P+PDYZX
OAhmi19hFEP0zQoWiW3TiMEVPlaqgtipPCp1t+jTeNNN3F+H4NG2DHJJZ3dPDr2J
CpABQKyS4MJQTUxhCIlXqAaA2I1pejzAv6fwsF66/zPFmyaTAJLDP+3WMQvCUUoZ
5A3qHgHNp6vBHXd13RNdQStLeprfQptA+z6XdiJPos348ecRj/u9id7v28dwxxsm
d9WA6oYwJ+Y/NcG2OP0Flyp3Zc3hymVsv5vhmhG2+EiIrxMn95k8ImsKLEhvUW3a
72CxlE6EaOMD4MuWyeGMS33c0vHwtAvEIE7M56R2FAl8EsUFwP2swaij0tEiemg=
=8MRV
-----END PGP SIGNATURE-----

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Python 3.x and bytes Corey Richardson <kb1pkl@aim.com> - 2011-05-17 15:50 -0400

csiph-web