473,387 Members | 1,501 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,387 software developers and data experts.

Question about "global" statement

ChrisWang
Hi,

I am having trouble understanding the use of 'global' variables I want to use a global variable with the same name as a parameter of a function. But I don't know how to use them at the same time. Here is a snippet of example code:

Expand|Select|Wrap|Line Numbers
  1. def foo (a):
  2.     global p
  3.     global a
  4.     p = a + 3  #here "a" will be reference to the global one
  5.  
  6. a = 1
  7. p = 0
  8. foo (a)
  9. print a, p
When I executed this script, Python said "SyntaxError: name 'a' is local and global". I don't think there is anything wrong with Python, but just want to find a way to use local and global variable in one function. In CPP, we can use :: prefix to indicate the variable is global. But I have no idea that how to implement it in Python. Could anybody tell me the solution?

Thanks!
Nov 20 '08 #1
4 3390
Smygis
126 100+
If you have to use global variables my favorite way is to crate a container for the global variables, like this:

Expand|Select|Wrap|Line Numbers
  1. >>> class gv: pass
  2. ... 
  3. >>> def foo():
  4. ...     gv.p = gv.a + 3
  5. ... 
  6. >>> gv.p, gv.a = 0, 1
  7. >>> foo()
  8. >>> print gv.a, gv.p
  9. 1 4
  10. >>> 
  11.  
Anyway what you are doing wrong is exactly what the error message says. There is two "a" and python gets confused as to whom of them you are referring to.
Expand|Select|Wrap|Line Numbers
  1. >>> def bar(a):
  2. ...     global a
  3. ...     print a
  4. ... 
  5. SyntaxError: name 'a' is local and global
  6.  
Solve it by using different variable names. This is why i prefer to create the container for global variables. Its to keep them separate form the other variables.
Nov 20 '08 #2
Good suggestion! Thanks!
Nov 21 '08 #3
I found another solution to this problem.

I can use a name space for the scoping.

__main__.a will be referenced to a global variable.

Anyway, I think container is better.
Nov 21 '08 #4
Curtis Rutland
3,256 Expert 2GB
Please enclose your posted code in [CODE] [/CODE] tags (See How to Ask a Question). Code tags preserve indention and uses a monospaced font.

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [CODE] [/CODE] tags in future.

MODERATOR
Nov 21 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: mrbog | last post by:
I have an array/hash that stores path information for my app. As in, what directory this is in, what directory that's in, what the name of the site is, what the products are called, etc. It's...
1
by: Chris Stromberger | last post by:
This doesn't seem like it should behave as it does without using "global d" in mod(). d = {} def mod(): d = 3 mod() print d
7
by: Lyn | last post by:
Hi and Season's Greetings to all. I have a question regarding the use of a qualifier word "Global". I cannot find any reference to this in Access help, nor in books or on the Internet. "Global"...
9
by: Javaman59 | last post by:
I saw in a recent post the :: operator used to reach the global namespace, as in global::MyNamespace I hadn't seen this before, so looked it up in MSDN, which explained it nicely. My question...
3
by: Pierre | last post by:
Hello, In an aspx page (mypage.aspx) from a web projet, I would like to get the value of a variable of the projet that is declared as public in a module. The variable can be called from...
5
by: dave | last post by:
If I have a class that hold, for instance, user settings that should be accessible to the entire program logic, what is a good paradigm to use? In C++, I would have made it a global object,...
2
by: Paul | last post by:
I am taking over an exisitng app. I have the following function on one page: function check($v) { global $user; return $user->validate($v, $v); } And it reference $user on the next page. ...
11
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global"...
1
by: Tom Berger | last post by:
Just a short question concerning exception handling.... All of my applications contain an event handling in the Main() like this one: static void Main() { // declare global exeption...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
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...

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.