View Single Post
  #10 (permalink)  
Old 03-30-2009, 03:49 AM
mark.seagoe@gmail.com
Guest
 
Posts: n/a
Default Re: Python print and types selection

On Mar 28, 1:47*pm, Scott David Daniels <Scott.Dani...@Acm.Org> wrote:
> mark.sea...@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...@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.

Reply With Quote