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

Discussion Overview

The discussion revolves around the use of the "global" keyword in Python, particularly in the context of functions and variable scopes. Participants explore its purpose, implications, and best practices related to modifying global variables from within functions.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests that the global keyword is primarily used to modify global variables when local and global variables share the same name.
  • Another participant provides an example demonstrating the difference between using global and local variables, indicating that without the global keyword, the local variable does not affect the global one.
  • A different viewpoint argues against the use of the global keyword, stating it leads to confusion and suggesting that variables should be passed in or out of functions instead.
  • There is a question about whether local variables returned from a function can be assigned to global variables, with some participants clarifying that this depends on the context of the calling code.
  • One participant emphasizes that marking a variable as global prevents the creation of a local variable with the same name within the function.

Areas of Agreement / Disagreement

Participants express differing opinions on the necessity and appropriateness of using the global keyword, with no consensus reached on its best practices or implications.

Contextual Notes

Some statements reflect assumptions about variable scope and the behavior of functions that may not be universally applicable, depending on the specific context of the code being discussed.

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
9K
  • · 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