[Python-3000] bytes: compare bytes to integer

Georg Brandl g.brandl at gmx.net
Sun Aug 12 19:41:40 CEST 2007


Bill Janssen schrieb:
>> I don't like the behaviour of Python 3000 when we compare a bytes strings
>> with length=1:
>>    >>> b'xyz'[0] == b'x'
>>    False
>> 
>> The code can be see as:
>>    >>> ord(b'x') == b'x'
>>    False
>> 
>> or also:
>>    >>> 120 == b'x'
>>    False
>> 
>> Two solutions:
>>  1. b'xyz'[0] returns a new bytes object (b'x' instead of 120) 
>>     like b'xyz'[0:1] does
>>  2. allow to compare a bytes string of 1 byte with an integer
>> 
>> I prefer (2) since (1) is wrong: bytes contains integers and not bytes!
> 
> Why not just write
> 
>    b'xyz'[0:1] == b'x'
> 
> in the first place?  Let's not start adding "special" cases.

Hm... I have a feeling that this will be one of the first entries in a
hypothetical "Python 3.0 Gotchas" list.

Georg


-- 
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.



More information about the Python-3000 mailing list