Python How to correctly use the "global" keyword in Python

  • Thread starter Thread starter fog37
  • Start date Start date
  • Tags Tags
    Global Python
AI Thread Summary
The discussion centers on the use of the global keyword in programming, particularly in Python. It clarifies that variables defined within a function are local and cannot be accessed outside of that function unless explicitly returned. Global variables, defined outside of functions, can be accessed anywhere in the program, including within functions. The global keyword is primarily used to modify a global variable when a local variable with the same name exists. However, it's noted that using global variables is often discouraged due to potential confusion and poor programming practices. Instead, passing variables in and out of functions is recommended for better clarity. The conversation also emphasizes that when a function returns a value, it does not automatically assign it to a global variable unless explicitly done so in the calling code. Overall, the consensus is that while the global keyword has its uses, it is generally better to avoid it in favor of clearer programming practices.
fog37
Messages
1,566
Reaction score
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
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.
 
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
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 BvU, fog37 and phyzguy
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
10
Views
2K
Replies
33
Views
3K
Replies
2
Views
2K
Replies
1
Views
7K
Replies
16
Views
2K
Replies
26
Views
3K
Replies
8
Views
2K
Back
Top