By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

What ought to persist after a program is run?

P: n/a
Here's a very simple program with an odd twist:
class Player(object):
def __init__(self,name):
self.name = name

hero = Player("A")
print "hero",hero

If I run it in IDLE and then type dir() at the prompt, I get
['Player', '__builtins__', '__doc__', '__name__', 'hero']
However, if I modify the program as follows
class Player(object):
def __init__(self,name):
self.name = name
def main():
hero = Player("A")
print "hero=",hero
main()

and then run it in IDLE and type dir at the prompt, I get['Player', '__builtins__', '__doc__', '__name__']


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

Thomas Philips
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

-Peter

** 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).
Jul 18 '05 #2

P: n/a
In article <b4**************************@posting.google.com >,
tk****@hotmail.com (Thomas Philips) wrote:
Here's a very simple program with an odd twist:
class Player(object):
def __init__(self,name):
self.name = name

hero = Player("A")
print "hero",hero

If I run it in IDLE and then type dir() at the prompt, I get
['Player', '__builtins__', '__doc__', '__name__', 'hero']
However, if I modify the program as follows
class Player(object):
def __init__(self,name):
self.name = name
def main():
hero = Player("A")
print "hero=",hero
main()

and then run it in IDLE and type dir at the prompt, I get['Player', '__builtins__', '__doc__', '__name__']


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


Because main(), like any function, binds the object to its
local namespace, not the "global" (module) namespace.

Each function has its own, separate namespace, every time
it's invoked. Ordinarily, the module namespace is mostly
populated with functions, classes etc. defined in the
module source (or typed in at the keyboard in your case.)
Functions use their own namespaces for scratch space,
and "return" the results of their computation, or modify
input parameters. This isolation simplifies interdependencies
between functions, so they're easier to manage as code gets
rewritten.

Donn Cave, do**@u.washington.edu
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.