[Python-ideas] Direct byte<->int conversions (was Re: bitwise operations on bytes)
alexandre at peadrop.com
Sat Aug 15 17:45:37 CEST 2009
[Oops, I forgot to CC python-ideas. Sorry Eric for the double post.]
On Sat, Aug 15, 2009 at 8:13 AM, Eric Eisner<ede at mit.edu> wrote:
> This patch seems very complete, with only the API to hammered out.
> Here is my summary of some options:
> method names:
> patch behavior: int.as_bytes / int.frombytes
> int.as_bytes / int.from_bytes
> int.asbytes / int.frombytes
In my patch, I chosen to use the name 'as_bytes' because it was
consistent with 'float.as_integer_ratio'. Similarly, I chosen the name
'frombytes' because it was consistent with 'float.fromhex'. I don't
mind the inconsistent use of the underscore in the names, but I admit
there is room for improvement. So, what do you think of
`int.frombytes` and `int.tobytes`?
> patch behavior: default flag: little_endian=False
> byteorder option accepting 'big' or 'little', this can also accept sys.byteorder
I like the byteorder option better.
I believe the byteorder option shouldn't default to use the native
byte-order however. As you mentioned, it would be a bad choice to
encourage the default behaviour to be platform-dependent. And since
the primary purpose of the API is long serialization, it would be
short-sighted to choose the option that cannot be used for
serialization as the default.
Whether it should default to 'little' or 'big' is pretty much an
arbitrary choice. In my patch, I choose to default big-endian since it
is the standard network byte-order. But maybe the option should
default to little-endian instead since it more widely used. In
addition, the patch is slightly more efficient with little-endian.
> patch behavior: default flag: signed=True
> maybe unsigned as the default?
Either is fine by me. The advantage with 'signed' as the default is
'signed' works with all longs (and not only with non-negative ones).
> byte length:
> patch behavior: fixed_length=None
> other names: length, bytelength
I still like `fixed_length` better than proposed alternatives. The
name `fixed_length` makes it clear that the returned object has a
fixed and constant length. And, I find `fixed_length=None` is more
telling than `length=None`.
More information about the Python-ideas