
Aug. 11, 2009
12:38 a.m.
On Tue, Aug 11, 2009 at 02:16, Ron Adam<rrr@ronadam.com> wrote:
The constructor for the int type already does base and string conversions, extending it to bytes seems like it would be natural.
int(bytes) # just like int(string) bytes = bytes(int) # calls int.__to_bytes__() to do the actual work.
The constructor for bytes currently accepts a single int as an argument, producing that many zero bytes. As far as I can tell this behavior is undocumented, but I don't know if that can be changed easily... The first thing I did when I tried out the bytes type in 3.x was to try to convert an int to a byte via the constructor, and the current behavior surprised me. -Eric