471,321 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Question about "global" statement


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
  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?

Nov 20 '08 #1
4 3191
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. >>> 
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
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.

Nov 21 '08 #5

Post your reply

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

Similar topics

11 posts views Thread by mrbog | last post: by
1 post views Thread by Chris Stromberger | last post: by
9 posts views Thread by Javaman59 | last post: by
1 post views Thread by Tom Berger | last post: by
reply views Thread by rosydwin | last post: by

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.