Here's a very simple program with an odd twist:
class Player(object):
def __init__(self,n ame):
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,n ame):
self.name = name
def main():
hero = Player("A")
print "hero=",her o
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 2 1191
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).
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,n ame): 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,n ame): self.name = name def main(): hero = Player("A") print "hero=",her o 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 interdependenci es
between functions, so they're easier to manage as code gets
rewritten.
Donn Cave, do**@u.washingt on.edu This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 capabilities, unfortunately. I'd like to know if it may
be possible to add a powerful macro system to Python, while
keeping its amazing syntax, and if it could be possible to
add Pythonistic syntax to Lisp or Scheme, while keeping all
of the...
|
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 persist (or send to a different process). I guess this is like what RPC does, in that it passes an ID of an interface function to a different process.
I think I remember that pointers to virtual member functions are actually just the offsets from the...
|
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 use them but why? Does
this enhance accessibility? Please provide reasons why one would want
to use these? Thanks
Burnsy
|
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 : %d\n",sizeof(unsigned char));
printf("short : %d\n",sizeof(short));
printf("unsigned short : %d\n",sizeof(unsigned short));
printf("int : %d\n",sizeof(int));
|
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 too much - that it's bad
programming practice. Is there an easy way to deal with this? I would like
to do things in the "Best Practices" way but at the same time I don't want
to make a federal case out of it.
This comes up over and over...
| |
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!!!
|
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 paper written on this
subject.
On the Expressive Power of Programming Languages, by Matthias
Felleisen, 1990.
http://www.ccs.neu.edu/home/cobbe/pl-seminar-jr/notes/2003-sep-26/expressive-slides.pdf
|
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. The computer actknoledges the
One care removal and warns me I have no virus program runing or instaled. BUT
the error: " first remove Wimdows one care program first" persist and stops
the Defender program from installing.
|
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 virtual functions, and functions you can call to instantiate and
delete objects of the type. You can then just call virtual functions on
the type."
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |