Scope of instantiated class

Peter Hansen peter at engcorp.com
Sun Jul 21 13:22:30 EDT 2002


Jeff Layton wrote:
> 
> >    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?)
> >
> Oops, as a follow-up the function the instantiates
> the class and the routine it returns to are in
> different modules.

Where the functions are is irrelevant in this case.

When you create an object ("instantiate a class") you
must bind it to a name ("assign it to a variable").  If
the name you use is local to the function, then when
the function exits the binding is removed and the object,
if no other names are by now bound to it, is destroyed.

If you have a particular goal in mind (as opposed to
just curiosity about the mysterious inner workings of
programming languages), then if you describe it someone
will be able to explain how to accomplish what you want
in a different way.

-Peter



More information about the Python-list mailing list