Basic python OOPs question

Gordon McMillan gmcm at hypernet.com
Mon Jun 14 22:44:36 EDT 1999


Daniel Mendyke asks:
> 
> Do I understand correctly that objects are removed
> from the name space when they go out of scope?

They are reference counted. Going out of scope is one way to decrease 
the ref count. Rebinding the reference is another ("obj = None").

> And that they go out of scope based on indentation?

Not exactly. An "if" statement, for example, does not have a new 
namespace. Functions and methods do.
 
> If so is there a default function called on 
> objects that go out of scope?  The __init__
> function is called when an object is created.
> Is there a function that is called when the
> object is destroyed?

Yes. It's __del__. It is called when the refcount drops to 0. It's 
also called at clean-up time, which can be troublesome, because 
things you refer to in __del__ may well have already been cleaned up. 
There's a FAQ entry on the subject.

- Gordon




More information about the Python-list mailing list