[Python-ideas] bitwise operations on bytes
Ron Adam
rrr at ronadam.com
Tue Aug 11 05:26:59 CEST 2009
Eric Eisner wrote:
> On Tue, Aug 11, 2009 at 02:16, Ron Adam<rrr at 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.
It seems that bytes are quite different here in 2.6 and 3.0.
In 2.6 and bytes is an alias for string(), so the bytes constructor
behavior just converts the int to a string.
ra at Gutsy:~$ python
Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> bytes(123)
'123'
>>> bytes
<type 'str'>
>>> help(bytes)
class str(basestring)
| str(object) -> string
|
| Return a nice string representation of the object.
| If the argument is a string, the return value is the same object.
|
(clipped)
Ron
More information about the Python-ideas
mailing list