Python print and types selection

mark.seagoe at gmail.com mark.seagoe at gmail.com
Sun Mar 29 23:49:13 EDT 2009


On Mar 28, 1:47 pm, Scott David Daniels <Scott.Dani... at Acm.Org> wrote:
> mark.sea... at gmail.com wrote:
> > ...
> > It appears that if I make the class a subclass of long...
> > class bignumber(long):
> >     def __init__(self, initval):
> >         self.val = initval
>
> > Then if I make a new class of subclass of bignumber...
> > class myclass(bignumber):
> >     def __init__(self, another_custom_class)
> >         bignumber.__init__(self, 0)
> >         do some stuff with another_custom_class
>
> > When I try to use this, I get an error sort of like this:
> > "TypeError: long() argument must be a string or a number, not
> > [whatever type another_custom_class is]"
>
> Remember that long is an immutable class (so you need to fiddle __new__,
> not __init__).  So, do something a bit more like:
>
>      class BigNumber(long):
>          def __repr__(self):
>              return '%s(%s)' % (type(self).__name__, self)
>
>      class HugeNumber(BigNumber):
>          def __new__(class_, something):
>              return BigNumber.__new__(class_, something * 3)
>
>   then you can do something like:
>      print 'dog = %r = %016X' % (HugeNumber(123), HugeNumber(123))
>
> Hope that helps.
>
> --Scott David Daniels
> Scott.Dani... at Acm.Org- Hide quoted text -
>
> - Show quoted text -

Thanks, Scott.

I'm not sure what the "something * 3" does?  Here is the printout:
dog = HugeNumber(369) = 0000000000000171
But I was able to put the concept into practice.




More information about the Python-list mailing list