[Python-Dev] Python 3.x and bytes

Michael Foord fuzzyman at voidspace.org.uk
Thu May 19 13:29:07 CEST 2011

On 19/05/2011 10:25, Łukasz Langa wrote:
> Wiadomość napisana przez Stefan Behnel w dniu 2011-05-19, o godz. 10:37:
>>> But why wouldn't "they" expect `b'de' + 1` to work as well in this case? If a 1-byte bytes is equivalent to an integer, why not an arbitrary one as well?
>> The result of this must obviously be b"de1".
> I hope you're joking. At best, the result should be b"de\x01".
The behaviour Stefan suggests is what some "weakly typed" languages like 
perl (and possibly php?) do, which masks errors and is rightly abhorred 
by Python programmers (although semantically not *so* different from 1 + 
1.0 == 2.0). I think it's safe to say that Stefan was joking.


>   But I don't think such construct should be allowed. Just like you can't do `[1, 2, 3] + 4`. I wouldn't ever expect that a single byte behaves like a sequence of bytes. In the case of bytes b'a' is obviously still a sequence of bytes, just happening to store a single one. Indexing should return a byte so I'm not surprised it returns a number. Slicing on the other hand returns a sub-sequence.
> However inconvenient, I find the current behaviour logical and predictable. A shortcut for b'a'[0] would obviously be nice but that's for python-ideas.


May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html

More information about the Python-Dev mailing list