Mystery Theater New Style Classes

Raymond Hettinger vze4rx4y at
Thu Jul 24 21:59:59 CEST 2003

"Bob Gailer"
> Predict the output:
> class A(int):
>      classval = 99
>      def __init__(self, val = 0):
>          if val:
>              self = val
>          else:
>              self = A.classval
> a=A(3)
> b=A()
> print a,b

The output would be 3 and 0.

I think the point is that the value binding occurs
in the int.__new__ classmethod and that self
already has its value when __init__ is called.
Assigning the local variable, self, has no effect.

The other tidbit is that int() with no args returns zero.

Raymond Hettinger

More information about the Python-list mailing list