[issue27923] PEP 467 -- Minor API improvements for binary sequences

Nick Coghlan report at bugs.python.org
Tue Oct 11 12:43:35 EDT 2016


Nick Coghlan added the comment:

Something else the PEP needs to be updated to cover is that in 3.5+ (and maybe even in 3.4 - I'm not sure when 'c' support landed in memoryview) you can write the following efficient bytes iterator:

    def iterbytes(data):
        return iter(memoryview(data).cast('c'))

And use it as so:

>>> data = b"123"
>>> for datum in iterbytes(data):
...     print(datum)
... 
b'1'
b'2'
b'3'

That will then work with any buffer exporter, not just bytes and bytearray, and since it's a function, you can easily make a similar function that's polymorphic on str -> str iterator and bytes-like -> bytes iterator.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27923>
_______________________________________


More information about the Python-bugs-list mailing list