[Python-3000] PEP 3137: Immutable Bytes and Mutable Buffer
Jeffrey Yasskin
jyasskin at gmail.com
Thu Sep 27 05:44:16 CEST 2007
On 9/26/07, Guido van Rossum <guido at python.org> wrote:
> ...
> Indexing
> --------
>
> **Open Issue:** I'm undecided on whether indexing bytes and buffer
> objects should return small ints (like the bytes type in 3.0a1, and
> like lists or array.array('B')), or bytes/buffer objects of length 1
> (like the str type). The latter (str-like) approach will ease porting
> code from Python 2.x; but it makes it harder to extract values from a
> bytes array.
Marcin was far more eloquent than I could hope to be, but I too prefer
indexing bytes to return a small int. My reasoning is a little more
academic: All iterable types except for str get simpler when you
iterate over them, so eventually you come to a type that isn't
iterable. It would be a shame to extend this misbehavior to bytes if
we have a chance to remove it.
For example, the recursive flatten() function gets more complicated
for each type that does this:
>>> list(flatten.flatten([1, [2, [3, [4, 5]]]]))
[1, 2, 3, 4, 5]
>>> list(flatten.flatten([1, [2, [3, ["str", 5]]]]))
[1, 2, 3, 's', 't', 'r', 5]
If all iterables iterated over a simpler type, we could use:
def flatten(iterable):
try:
for elem in iterable:
for elem in flatten(elem):
yield elem
except TypeError:
# Not iterable
yield iterable
but with strings, you need
def flatten(iterable):
try:
for elem in iterable:
if isinstance(elem, str) and len(elem) == 1:
yield elem
else:
for elem in flatten(elem):
yield elem
except TypeError:
# Not iterable
yield iterable
and another special case for each similar type.
Comparisons with literal bytes could be done with:
for b in bb:
if b == b'x'[0]: ...
or perhaps
if b == int(b'x'): ...
but you're right that's not ideal.
--
Namasté,
Jeffrey Yasskin
More information about the Python-3000
mailing list