What ought to persist after a program is run?

Peter Hansen peter at engcorp.com
Wed Jun 16 20:59:50 CEST 2004

Thomas Philips wrote:

> Why does 'hero' not appear in the workspace directory when main() is
> invoked and vice versa?

Variables are considered local to functions unless explicitly
specified otherwise.  Since you don't say anything about hero,
it is local and therefore does not persist after main() completes.

What you might be looking for is the global keyword.  If you
put "global hero" anywhere in main() before you use the name
hero, any references to it are treated as global to the module
instead of local, and it will then appear in the "workspace
directory"** after main() completes.


** By that term, I assume you mean it appears when you type dir()
at an interactive prompt or something.  The term has no meaning
to me (perhaps because I don't use IDLE).

More information about the Python-list mailing list