[Python-Dev] Python 3.x and bytes
ethan at stoneleaf.us
Wed May 18 21:29:47 CEST 2011
Ethan Furman wrote:
> Greg Ewing wrote:
>> As for
>>> --> some_other_var == b'd'
>> there ought to be a literal for specifying an integer
>> using an ascii character, so you could say something like
>> if some_other_var == c'd':
>> which would be equivalent to
>> if some_other_var == ord(b'd')
>> but without the overhead of computing the value each time
>> at run time.
> Given that we can't change the behavior of b'abc', that would be
> better than what we have.
Here's another thought, that perhaps is not backwards-incompatible...
some_var == b'd'
At some point, the bytes class' __eq__ will be called -- is there a
reason why we cannot have
1) a check to see if the bytes instance is length 1
2) a check to see if
i) the other object is an int, and
2) 0 <= other_obj < 256
3) if 1 and 2, make the comparison instead of returning NotImplemented?
This makes sense to me -- after all, the bytes class is an array of ints
in range(256); it is a special case, but doesn't feel any more special
than passing an int into bytes() giving a string of that many null
bytes; and it would get rid of the, in my opinion ugly, idiom of
some_var[i:i+1] == b'd'
It would also not require a new literal syntax.
More information about the Python-Dev