Scope of instantiated class

Carl Banks imbosol at vt.edu
Mon Jul 22 14:25:10 EDT 2002


Jeff Layton wrote:
> Hello,
> 
>    If I instantiate a class in a function,
> leave the function via a return and then
> go back to the same function, will the
> instantiation of the class still be there
> or is it local to the function so it is
> "destroyed"? (Does this make any sense?)


It seems that you are asking whether the class will be recreated every
time you call the function.  The answer is yes, and classes created by
different calls to the function will be distinct classes.

However, it doesn't mean the class is "destroyed" when the function
exits.  The class will continue to exist after the function exits if
there are any references to the class that still exist.  For example,
if the function returns an instance of the nested class, then the
class will not be destroyed because the instance contains a reference
to the class.

But, even if the nested class still exists when you call the function
again, the function will construct a new and distinct class.


-- 
CARL BANKS
http://www.aerojockey.com



More information about the Python-list mailing list