How to correctly use the "global" keyword in Python

In summary, the keyword global is used to modify global variables inside a function and allow access to them from other parts of the program. However, it is generally not recommended as it can lead to confusion and is not good programming practice. The "return" statement can be used to assign the value of local variables to external global variables without needing to use the "global" keyword.
  • #1
fog37
1,568
108
TL;DR Summary
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
  • #2
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 fog37
  • #3
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 pbuk and fog37
  • #4
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)
 
  • #5
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 BvU, fog37 and phyzguy

1. What is the purpose of the "global" keyword in Python?

The "global" keyword in Python allows variables to be accessed and modified from within any scope of the code, including inside functions. This means that the variable can be used and changed in multiple functions without having to pass it as an argument each time.

2. How do you declare a variable as global in Python?

To declare a variable as global, you need to use the "global" keyword followed by the name of the variable. For example, "global x" will declare the variable "x" as global.

3. Can you use the "global" keyword inside a function?

Yes, the "global" keyword can be used inside a function to declare a variable as global. However, it is recommended to declare all global variables at the beginning of the code for better readability.

4. What happens if you try to modify a global variable without declaring it as global first?

If a variable is modified without declaring it as global first, a new local variable with the same name will be created. This local variable will only exist within the scope of the function and will not affect the global variable with the same name.

5. Can you have multiple global variables with the same name?

No, you cannot have multiple global variables with the same name. This will result in a syntax error as variables must have unique names within the same scope.

Similar threads

  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
4
Views
389
  • Programming and Computer Science
Replies
7
Views
487
  • Programming and Computer Science
Replies
25
Views
3K
Replies
6
Views
655
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top