[Python-ideas] bytes indexing behavior
Sjoerd Job Postmus
sjoerdjob at sjoerdjob.com
Mon Jun 6 16:09:01 EDT 2016
I like this idea!
>>> k = b'hello world'
>>> v = k[0]
>>> type(v)
<class 'byte'>
>>> ord(v)
104
>>> v + 5
__main__:1: DeprecationWarning: bytes items will become byte instances. Use ord().
109
On the other hand, maybe 'hello'[0] should be a ch(a)r instead of str?
The 'wrapping' class could be a `bytearray`?
>>> k = bytearray(b'hello')
>>> k[0]
104
>>> k[2:4]
bytearray(b'll')
The only 'downside' is that a bytearray is mutable.
> On 6 Jun 2016, at 21:35, Neil Schemenauer <nas-pythonideas at arctrix.com> wrote:
>
>> On 2016-06-04, Guido van Rossum wrote:
>> The bytes -> int behavior is widely considered a mistake. We're just
>> not able to fix it without yet another round of layoffs ^W
>> deprecations. And I'm not ready for that -- not even Python 4 should
>> be allowed to change this unilaterally. Though maybe we could do
>> something with a __future__ import.
>
> Maybe the following would work:
>
> - add a new method to 'bytes' that returns a view object with the
> current index/iteration behavior
>
> - enable a deprecation warning for code that uses indexing/iteration
> on bytes
>
> - when sufficient time has passed, revert to Python 2 behavior for
> indexing/iteration
>
> Another, probably crazy and unworkable idea:
>
> - have bytes indexing/iteration return a special type that behaves like
> an int or length one byte.
>
> - ord() of this object would return a real int
>
> - code that utilizes this object as an int would generate a warning
> (suggest adding an ord() call to fix code).
>
> - eventually just return length one byte strings
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
More information about the Python-ideas
mailing list