[Tutor] Object Destruction

Gregg T. Geiger gt_geiger@yahoo.com
Wed, 24 Jan 2001 18:27:35 -0800 (PST)

What are scope rules for class objects and when are they
destroyed, specifically with regards to for loops?

That is, if a class Spam is defined somewhere and used in a
for loop as follows:

for i in [0, 1, 2, 3, 4]:
  spam = Spam()
  # end of for loop

will an object be created and destroyed with each pass
through the loop or does each instance remain "alive"?  In
a comparable C++ loop

for (int i = 0; i < 5; ++i){
  Spam spam;

the destructor is called 5 times.  In my own experience
with Python, files opened in Spam are not closed each time
through the loop unless explicitly closed within the class.

Any insight(s) would be appreciated.



Do You Yahoo!?
Yahoo! Auctions - Buy the things you want at great prices.