What ought to persist after a program is run?

T

Thomas Philips

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
 
P

Peter Hansen

Thomas said:
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).
 
D

Donn Cave

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, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top