[Tutor] Changing a class into a subclass
Kent Johnson
kent37 at tds.net
Fri Mar 25 04:19:41 CET 2005
Ismael Garrido wrote:
> But there's something that I couldn't understand. In the following code,
> my guess would be that "I'm back from the death" would never get
> printed... but it is... and twice! Why?
It's printed every time B.pong() is called, just as "Pong" is. You print each one twice - no mystery!
Kent
>
> >>> class A:
> def pong(self):
> print self.__class__
> print "Ping!"
> self.times -=1
> if self.times >= 0:
> self.__class__ = B
> self.pong()
>
> >>> class B:
> def pong(self):
> print self.__class__
> print "Pong!"
> self.times -=1
> self.__class__ = A
> self.pong()
> print "I'm back from the death"
>
> >>> a = A()
> >>> a.times = 3
> >>> a.pong()
> __main__.A
> Ping!
> __main__.B
> Pong!
> __main__.A
> Ping!
> __main__.B
> Pong!
> __main__.A
> Ping!
> I'm back from the death ##Weird!
> I'm back from the death
> >>> a.__class__
> <class __main__.A at 0x00C1AF00>
>
>
> Thanks,
> Ismael
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list