[Python-ideas] Python 3.x and bytes
Bruce Leban
bruce at leapyear.org
Tue May 24 02:18:41 CEST 2011
I like c'x'. It's easy to read and very explicitly constant and clear what
the value is 'x'. (Some other letter instead of 'c' would be fine as well.)
I don't like this:
> if bytesdata[i] == 121: # ord('x')
because it looks a heck of a lot like:
> if bytesdata[i] == 120: # ord('x')
and only one of those is correct. That's a very easy bug to miss. I like it
even less without the comment.
I don't care for:
> if bytesdata[i] == ord('x'):
because while ord is a builtin, it's not invulnerable to being changed. In
contrast, string constants and numbers are truly constant.
I recognize that the compiler can optimize:
> if bytesdata[i] == b'x'[0]:
but that looks like chicken scratches to me.
Someone suggested using 0'x' which I don't quite get. It looks too much like
0x to me and the I've always read the leading zero to mean 'this is a
number'.
Also, this was raised in the context of bytes and not all characters fit in
a byte. So
c'Δ'
ord('Δ')
work but
b'Δ'[0]
won't.
Is there a learning curve? Yes, but minor IMHO and if you don't know it,
it's obvious when you see it that you don't know it.
--- Bruce
Follow me: http://www.twitter.com/Vroo
Latest tweet: SO disappointed end of the world didn't happen AGAIN!
#y2k<http://twitter.com/#!/search?q=%23y2k>
#rapture <http://twitter.com/#!/search?q=%23rapture> Now waiting for 2038!
#unixrapture <http://twitter.com/#!/search?q=%23unixrapture>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110523/b6ccbe65/attachment.html>
More information about the Python-ideas
mailing list