473,320 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

What ought to persist after a program is run?

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
2 1172
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
2
by: Tom | last post by:
I have some virtual member functions like this: struct IBlah { virtual void f1() = 0; virtual void f2() = 0; } I want to get a run-time identifier for one of these functions that I can...
33
by: bissatch | last post by:
Hi, I fully understand the purpose of an alt attribute within a <img> tag but why would you use a title or summary attribute within, for example, a <p> tag. I have read books recommending that I...
35
by: Sunil | last post by:
Hi all, I am using gcc compiler in linux.I compiled a small program int main() { printf("char : %d\n",sizeof(char)); printf("unsigned char : ...
17
by: Woody Splawn | last post by:
I am finding that time after time I have instances where I need to access information in a variable that is public. At the same time, the books I read say that one should not use public variables...
2
by: Protoman | last post by:
What does this do? int& fn(int& arg){arg++;return arg;} //later int y; fn(y)=5; Why can you do it? What do you use it for? Thanks!!!
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
1
by: =?Utf-8?B?V2luIERlZmVuZGVyIEluc3RhbGF0aW9uIGVycm9y | last post by:
I atempted to instal Windows Defender program. But a error: " first remove Windows one care program first" I uninstaled the program via the uninstal wizard & rebooted my HP Media Center 2005 xp....
13
by: Anonymous | last post by:
On MS site: http://msdn2.microsoft.com/en-us/library/esew7y1w(VS.80).aspx is the following garbled rambling: "You can avoid exporting classes by defining a DLL that defines a class with...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.