How to correctly use the "global" keyword in Python

  • Context: Python 
  • Thread starter Thread starter fog37
  • Start date Start date
  • Tags Tags
    Global Python
Click For Summary
SUMMARY

The "global" keyword in Python is used to modify global variables from within a function, particularly when a local variable and a global variable share the same name. If a variable is declared as global inside a function, all references to that variable within the function will refer to the global variable, preventing the creation of a local variable with the same name. Best practices suggest avoiding the use of global variables due to potential confusion and namespace issues. Instead, it is recommended to pass variables in and out of functions as needed.

PREREQUISITES
  • Understanding of Python functions and their scopes
  • Familiarity with variable types: local vs. global
  • Knowledge of Python's return statement functionality
  • Basic grasp of programming best practices regarding variable management
NEXT STEPS
  • Explore Python's function scope and variable lifetime
  • Learn about Python's namespaces and how they affect variable access
  • Investigate alternatives to global variables, such as using classes or passing parameters
  • Study Python's built-in functions and their implications on variable scope
USEFUL FOR

Python developers, software engineers, and anyone looking to improve their understanding of variable scope and best practices in programming.

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!
 
Technology 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.
 
  • 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.
 
  • 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).
 
  • Like
Likes   Reactions: BvU, fog37 and phyzguy

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 26 ·
Replies
26
Views
3K
  • · Replies 25 ·
Replies
25
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K