Strange behaviour: subclass of int returns 3 when constructed with long
aleax at aleax.it
Sun Feb 9 16:43:14 CET 2003
Gerrit Holl wrote:
> 0 >>> class A(int): pass
> 0 ...
> 1 >>> int(sys.maxint)
> 2 >>> A(sys.maxint)
> 3 >>> int(sys.maxint+1)
> 4 >>> A(sys.maxint+1)
> 5 >>> A(sys.maxint+42)
> 6 >>> int(A(sys.maxint+1))
In Python 2.3a1, this behavior is indeed reproducible on my
> how can this behaviour be explained? Shouldn't A behave identical to int?
int has acquired some magic to generate long instead when needed,
as in your statement number 3. It apppears the magic isn't
smoothly inherited in 2.3a1. You may want to signal this bug
in the sourceforge bug tracker.
I think the problem is with int.__new__ being specialcased ONLY
when the class it receives as its argument is int itself -- e.g.:
def __new__(cls, x): return int.__new__(cls, x)
def __new__(cls, x): return int.__new__(int, x)
the first case exhibits the bug, the second one doesn't --
so int.__new__ does seem to be the culprit here. It may
be using (the C equivalent of) 'cls is int' to condition
its magic, perhaps (haven't checked this in the 2.3a1
Python C sources).
More information about the Python-list