verilog like class w/ bitslicing & int/long classtype

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat Jan 31 06:42:14 EST 2009


On Fri, 30 Jan 2009 21:59:34 +0100, Stef Mientki wrote:

> Marc 'BlackJack' Rintsch wrote:
>> On Fri, 30 Jan 2009 00:25:03 +0100, Stef Mientki wrote:
>>
>>
>>> try this:
>>>
>>> class MyRegClass ( int ) :
>>>   def __init__ ( self, value ) :
>>>     self.Value = value
>>>   def __repr__ ( self ) :
>>>     line = hex ( self.Value )
>>>     line = line [:2] + line [2:].upper()
>>>     return line
>>>     
>>>     
>>     def __repr__(self):
>>         return '0x%X' % self.value
>>
>>
>>>   __str__ = __repr__
>>>     
>>>     
>> This is unnecessary, the fallback of `__str__` is `__repr__` already.
>>
>>
> well this is what my Python is doing:
> 
> without  _str__ = __repr__
>  >>print shadow_register
> 170
> 
> with  _str__ = __repr__
>  >>print shadow_register
> 0xAA

Then don't inherit from `int`.  Why are you doing this anyway?  If you 
inherit from `int` you shouldn't store the value in an extra attribute 
but use the `int`\s value.  This way you have one value used by the `int` 
methods and one used by your own methods.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list