Python 3.x and bytes

Corey Richardson kb1pkl at aim.com
Tue May 17 15:50:13 EDT 2011


-----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-----



More information about the Python-list mailing list