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


Groups > comp.lang.python > #5611

Re: Python 3.x and bytes

Date 2011-05-17 13:55 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: Python 3.x and bytes
References <4DD2C2A5.3080403@stoneleaf.us> <4DD2D175.30506@aim.com>
Newsgroups comp.lang.python
Message-ID <mailman.1711.1305665007.9059.python-list@python.org> (permalink)

Show all headers | View raw


Corey Richardson wrote:
> -----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'

Not so.

--> huh = b'abcedfg'
--> huh[3]
101

It's a list of int's.

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

Nope.  Apparently, it's not well documented.  If you check PEP 358 
you'll find it.

~Ethan~

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


Thread

Re: Python 3.x and bytes Ethan Furman <ethan@stoneleaf.us> - 2011-05-17 13:55 -0700

csiph-web