3

I am using some variable in multiple functions.
This includes changing the variable values by each of those functions.
I already declared the variable as 'global' in the first function.
Should I declare this variable again and again as global in each function (and this will not overwrite the first global variable I declared in the first function) or I should not declare it again as global in all those functions (but the local variables there still will be seen as global since I already declared this variable so first time)?

4
  • 3
    The global keyword tells the parser per function that a name shouldn't be treated as a local when assigned to. Commented Jun 2, 2014 at 15:50
  • @MartijnPieters so I should declare it as global in each function and the parser will understand that all these global variables are the same one? Commented Jun 2, 2014 at 15:54
  • 3
    Normally any name you bind in a function (assign to, use as a function argument, use in an import statement in the function body, etc.) is seen by the parser as a local. By using the global keyword, the parser will instead generate bytecode that'll look for a global name instead. If you have multiple functions that assign to the global, you'll need to declare that name global in all those functions. They'll then look up the name in the global namespace instead. Commented Jun 2, 2014 at 16:01
  • Thank you @MartijnPieters You could write this as an answer, not as a comment & I would give you all the points.... Commented Jun 2, 2014 at 16:06

3 Answers 3

10

You can declare a variable as global in each function definition. Here's an example:

def f():
    global x
    x = 2
    print x
    x +=2 
    # This will assign a new value to the global variable x

def g():
    global x
    print x
    x += 3
    # This will assign a new value to the global variable x

f()
# Prints 2
g()
# Prints 4
print x
# Prints 7
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, I think I understand it now. I've edited the answer to more directly answer your question.
Perfect... Saved my day.
5

The global keyword tells the parser per function that a name shouldn't be treated as a local when assigned to.

Normally any name you bind in a function (assign to, use as a function argument, use in an import statement in the function body, etc.) is seen by the parser as a local.

By using the global keyword, the parser will instead generate bytecode that'll look for a global name instead. If you have multiple functions that assign to the global, you'll need to declare that name global in all those functions. They'll then look up the name in the global namespace instead.

See the global statement documentation:

The global statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals.

and the Naming and Binding documentation:

If a name is bound in a block, it is a local variable of that block. If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a free variable.

1 Comment

thank you @Martijn Pieters. From the documentation above I would not be sure about all these. Now, after your explanations I understand this much better ;)
-2

Should I declare this variable again and again as global in each function

You should not have any global variables at all, and put these variables and functions into a class.

4 Comments

I think this is impossible in my code but I will take a thought in this direction
@Eliyahu It's not impossible in your code. It literally never is.
You are right but I currently prefer this way. It seems to me more suitable and easier in my circumstances.
@Eliyahu You're wrong. It's never more suitable, and you've already found the reason why it's more difficult to deal with global variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.