[Python-ideas] Direct byte<->int conversions (was Re: bitwise operations on bytes)

Nick Coghlan ncoghlan at gmail.com
Sat Aug 15 17:08:26 CEST 2009

Eric Eisner wrote:
> One option would be to have the defaults of int.as_bytes mirror the
> hex builtin function, eg big endian and unsigned. This way it could be
> more consistent with related functionality already in the core.

Having the byte conversion mirror the hex builtin is probably the least
arbitrary style guide we could come up with for the defaults.

The other option would be to not *have* defaults for either of these
settings and force the programmer to make a deliberate choice. Then once
it has been out in the field for a while and we have evidence about the
way people use it, add sensible defaults in the following release.

I would prefer either of those two options to attempting to just guess
what the appropriate defaults would be in the absence of a wide
selection of use cases.

I also like the idea of using sys.byteorder as the model for the
byteorder selection option (i.e. byteorder=sys.byteorder to select
native layout and 'big'/'little' to choose a specific one)

I think Mark also makes a good point that in cases where the size
doesn't matter, marshal or pickle is probably a better choice than this
direct conversion so +0 on requiring an explicit size on the conversion
to bytes. Again, this is a case where *adding* a default later would be
easy, but changing a default or removing a variable size feature would
be difficult.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list