integer to binary...

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sun Jun 4 23:11:36 EDT 2006


Grant Edwards a écrit :
> On 2006-06-02, Bruno Desthuilliers <bdesth.quelquechose at free.quelquepart.fr> wrote:
> 
>>Grant Edwards a écrit :
>>
>>>On 2006-06-01, nicolasg at gmail.com <nicolasg at gmail.com> wrote:
>>>
>>>>does anyone know a module or something to convert numbers like integer
>>>>to binary format ?
>>>
>>>They _are_ in binary format.
>>
>>Not really.
> 
> Yes, really.

No, not really.

>  Otherwise the bitwise boolean operations you
> demonstrated wouldn't work as shown.

Ho yes ?

> 
>>>>>(7).__class__
>>
>><type 'int'>
>>
>>>>>dir((7))
>>
>>['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
>>'__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
>>'__floordiv__', '__getattribute__', '__getnewargs__', '__hash__',
>>'__hex__', '__init__', '__int__', '__invert__', '__long__',
>>'__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__',
>>'__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__',
>>'__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__',
>>'__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__',
>>'__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__',
>>'__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']
>>
> 
> The fact that they impliment the xor operator is pretty much
> proof that integers are

... objects, instance of the int class. Not really what I'd call "binary 
format" !-)

Now if you go that way, it's of course true that everything on a 
computer ends up in a binary format.... It's true.

> stored in binary format -- xor is only
> defined for binary numbers.
> 
class Prisonner(object):
   def __xor__(self, other):
     return "I'm not a (binary) number, I'm a free man"

The fact that an object implements the xor operator is pretty much proof 
that the guy that wrote the class decided to implement the xor operator !-)

Grant, I of course agree that, *for practical means*, one can consider 
that Python's integer are "already in binary format" - for a definition 
of "binary format" being "you can do bitwise ops on them". But the truth 
is that Python integers are objects (in the OO meaning) holding integer 
values - not integer values themselves.



More information about the Python-list mailing list