Bytes indexing returns an int

Ervin Hegedüs airween at gmail.com
Tue Jan 7 06:53:04 EST 2014


hi,

On Tue, Jan 07, 2014 at 10:13:29PM +1100, Steven D'Aprano wrote:
> Does anyone know what the rationale behind making byte-string indexing
> return an int rather than a byte-string of length one?
> 
> That is, given b = b'xyz', b[1] returns 121 rather than b'y'.
> 
> This is especially surprising when one considers that it's easy to extract
> the ordinal value of a byte:
> 
> ord(b'y') => 121

Which Python version?

http://docs.python.org/2/reference/lexical_analysis.html#strings
"A prefix of 'b' or 'B' is ignored in Python 2;"

if you want to store the string literal as byte array, you have
to use "bytearray()" function:

>>> a = bytearray('xyz')
>>> a
bytearray(b'xyz')
>>> a[0]
120
>>> a[1]
121


http://docs.python.org/2/library/stdtypes.html
5.6. Sequence Types


hth,


a.




More information about the Python-list mailing list