Mystery Theater New Style Classes

Terry Reedy tjreedy at udel.edu
Thu Jul 24 14:26:20 EDT 2003


"Bob Gailer" <bgailer at alum.rpi.edu> wrote in message
news:mailman.1059062857.29503.python-list at python.org...
> Predict the output:
>
> class A(int):
>      classval = 99
>      def __init__(self, val = 0):
>          if val:
>              self = val
>          else:
>              self = A.classval

I am not sure what the point is.  However,

1.  Rebinding the local variable 'self' (or any other local variable)
only affects further code within the function, which there is not any.
So the above conditional is equivalent to 'pass'.

2.  Because ints are immutable, the value of a new int is set within
int.__new__, which is not overwritten.  Thus, the above __init__ is
irrelevant.  In particular, the value of an A object has nothing to do
with the defaulted parameter 'val=0'.

> a=A(3)

3. This is equivalent to a=int(3).  No surprises.

> b=A()

4. This is equivalent to b = int().  I thought this might raise an
exception.  But after seeing the result (0). I remember Guido's
decision (and a recent PyDev discussion thereof) that callable builtin
types should, if possible, return a null value when getting a null
input.  Hence

>>> int(), long(), float(), tuple(), list(), dict()
(0, 0L, 0.0, (), [], {})

> print a,b
3,0

5. Result is same if 'val=0' is changed to 'val=9' or anything else.

> [from OP's self followup] The question this leads to is: how does
one access the value of such a class within a class method?

Since there is no class method presented, this make not much sense.
Bob: were you aware of 4.) above when you posted this?

Terry J. Reedy






More information about the Python-list mailing list