Mystery Theater New Style Classes

Bob Gailer bgailer at alum.rpi.edu
Thu Jul 24 15:26:23 EDT 2003


At 02:26 PM 7/24/2003 -0400, Terry Reedy wrote:


>"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?

There's a lot I was not aware of. Your reply and Bengt's have givem me what 
I needed.

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625
-------------- next part --------------

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.500 / Virus Database: 298 - Release Date: 7/10/2003


More information about the Python-list mailing list