Does python hate cathy?

Edward A. Falk falk at green.rahul.net
Tue Mar 25 06:09:58 CET 2008


Interestingly, if you change

  swaroop = Person('Swaroop', 'M')
  swaroop.sayHi()
  swaroop.howMany()
  kalam = Person('Abdul Kalam', 'M')
  kalam.sayHi()
  kalam.howMany()
  cathy = Person('Catherine', 'F')
  cathy.sayHi()
  cathy.howMany()
  swaroop.sayHi()
  swaroop.howMany()

to

  def main():
    swaroop = Person('Swaroop', 'M')
    swaroop.sayHi()
    swaroop.howMany()
    kalam = Person('Abdul Kalam', 'M')
    kalam.sayHi()
    kalam.howMany()
    cathy = Person('Catherine', 'F')
    cathy.sayHi()
    cathy.howMany()
    swaroop.sayHi()
    swaroop.howMany()
    return 0


  if __name__ == "__main__":
    sys.exit(main())


The problem goes away.  (This is a good coding practice in any event.)

As others have pointed out, the order in which local variables are deleted
is undefined.  It looks to me as if the class Person got deleted before
Catherine did. 

Adding

  del kalam
  del cathy
  del swaroop

to the end of the program did fix the problem, presumably by forcing
kalam, cathy, and swaroop from being deleted before Person.

However, Adding

	self.myclass = Person

to the __init__() method didn't stop the problem.  I thought it might, because
then each of the objects would have held a reference to Person.  Actually, I would
have thought they'd hold a reference by merely existing, so is this not a bug?

-- 
	-Ed Falk, falk at despams.r.us.com
	http://thespamdiaries.blogspot.com/



More information about the Python-list mailing list