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@python.org http://mail.python.org/mailman/listinfo/tutor


_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Reply via email to