Python print and types selection
Scott David Daniels
Scott.Daniels at Acm.Org
Sat Mar 28 16:47:46 EDT 2009
mark.seagoe 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.Daniels at Acm.Org
More information about the Python-list
mailing list