How to correctly use the "global" keyword in Python

  • Context: Python 
  • Thread starter Thread starter fog37
  • Start date Start date
  • Tags Tags
    Global Python
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 2K views
fog37
Messages
1,566
Reaction score
108
TL;DR
How to correctly use the "global" keyword in Python
Hello,

I am learning about functions and namespaces and I have a question about the keyword global.

Variables defined inside a function are local variables and cannot be used outside of the function by the rest of the program (by using the return keyword, we can assign the value of a local variable to an external variable which is a global variable).

Global variables are defined inside the main program and outside of functions. However, they can be used everywhere in the program, even by the functions. So what is the global for? I think the keyword global is used in the case when a local variable and a global variable have the same name and we want to use/change the specific external global variable directly inside the function itself...
If external and internal variables to a functions have different names, the problem does not arise and there is no need to use global...

Is that correct?

Thank you!
 
Physics news on Phys.org
Well, I think the main use is to just modify global variables. A typical example is something like:
Python:
x = 0
def f():
    x = 1

def g():
    global x
    x = 2

print(x)
f()
print(x)
g()
print(x)
You can try to guess what will be printed.
 
Reply
  • Like
Likes   Reactions: fog37
I think the best answer as to how to use the "global" keyword is - don't. It's a lazy way to get access to a variable which is inside a class or function from another class or function. If there is more than one variable with the same name, it is very easy to get confused. You should pay attention to the namespaces and pass a variable in or out of a function if you need access to it. I know of no examples where you need to use the "global" keyword to accomplish what you want to accomplish.
 
Reply
  • Like
Likes   Reactions: pbuk and fog37
Point taken.

When we use return in a function, the variables in the return statement are local variables, I believe. So is it correct to say/think that the content of those local variables, through the return, get assigned to a global variables when we use the function?

For example:
Python:
def function(a):
    x=3*a
    return x

The variable x is a local variable. But when we use the function (see below), the variable b is a global variable that contains what the x variable did...

Python:
b= function(3)
 
fog37 said:
what is the global for?

To allow you to modify a global variable inside a function. Compare these two snippets of code:

Python:
>>> a = 'a'
>>> def test():
...     a = 'b'
...
>>> test()
>>> a
'a'

Python:
>>> a = 'a'
>>> def test():
...     global a
...     a = 'b'
...
>>> test()
>>> a
'b'

Generally, as has been mentioned, this is not good programming practice.

fog37 said:
I think the keyword global is used in the case when a local variable and a global variable have the same name

No. If a variable is marked "global" inside a function, it is impossible to define a local variable inside that function with the same name, because every reference to that variable name inside the function will automatically refer to the global variable with that name.

If a variable is not marked "global" inside a function, then yes, you can define a local variable with the same name as a global variable; that local variable will simply get thrown away when the function exits. You can use "return" inside the function to return the value of that global variable to whatever code called the function, but of course you don't need the "global" keyword to do that.

fog37 said:
is it correct to say/think that the content of those local variables, through the return, get assigned to a global variables when we use the function?

Not in general, no, because the code calling the function might not be global code (i.e., at the module level); it might be code inside another function. The value of the variable returned by the "return" statement gets assigned to whatever it gets assigned to in the calling code (it might not even get directly assigned to a variable, but instead used in an expression or another function call).
 
Reply
  • Like
Likes   Reactions: BvU, fog37 and phyzguy