Subclassing types and __init__ problems

Lucio Torre lucio at movilogic.com
Tue Mar 25 10:21:23 EST 2003


Hello all,

I have subclassed int and i am having some problems.

Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> class myclass(int):
...     def __init__(self, param):
...             print param
...
 >>> i = myclass(1)
1
 >>> i = myclass([1,2,3])
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: int() argument must be a string or a number
 >>>

I imagine this is an issue with the types/class unification and stuff.

My questions are:
is the previous code supposed to call int.__init__?
how can i make a class so that isinstance(myclass, int) is 1, but im not 
   forced to call int.__init__?

Thanks in advance,

Lucio.






More information about the Python-list mailing list