- 1

How do I create global variables in python without the "gloabal" keyword?

I create many variables that I want to access from different functions and classes. How do I do that without resorting to using the "global" keyword?

18th May 2019, 9:32 AM
Teddy Okello
Teddy Okello - avatar
15 Réponses
+ 4
You are wrong in that you need global variables and 'global' them to change them. The clean, most conservative way is to do it as I explained: passing as an argument. For changing the value, you use the second door of the function: the return value! def f(n): # do stuff with n return n # which is modified You call it like this: value = f(value) f is called with the value as it is; then what the function returns (which is something different) is stored in value. And as Anna said, if it is a *mutable object* you passed, like a list or a dictionary, you can actually change it in the function using the methods of that object. def f(some_list): some_list.append(1) Here, the list in the global scope is changed from the function.
18th May 2019, 10:08 AM
HonFu
HonFu - avatar
+ 3
Every variable that you define globally (not in a class or function) can be read from everywhere. Only if you want to reassign the name, you need to use global. But you should rather pass values as argument instead of reading them from the global sphere.
18th May 2019, 9:36 AM
HonFu
HonFu - avatar
+ 3
A variable is global if it is declared in the global scope, i.e. outside of all classes and functions. The "global" keyword isn't related to the declaration of global variables but it means that a variable that is used inside a function and has the same name as a global variable does indeed relate to the global variable and is not a local variable. a = 5 # global variable def f(): a = 17 # no "global" keyword: a is a local variable def g(): global a a = 42 # changes the global variable P.S. @"I don't want to use global because it is not professional and brings confusion" Huh?
18th May 2019, 9:46 AM
Anna
Anna - avatar
+ 2
Why do you not want to use global ?
18th May 2019, 9:33 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Teddy Okello how is it not professional ? And how does it bring confusion ? It does exactly what it says, globalize variables, I don't find it confusing at all but rather very easy and simple.
18th May 2019, 9:44 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
You can define a function like this: def f(): print(n) Then the value n will be read from the globals. When your code becomes larger that will be hard to maintain: You never know which is called from where and it becomes hellish to find a mistake. You can also define it like this: def f(n): print(n) Now you have to explicitly give n to the function. If you always only use in a function what was explicitly given to the function, it becomes easier to find a mistake because you know exactly where to check: At the door (parameter list).
18th May 2019, 9:45 AM
HonFu
HonFu - avatar
+ 2
Nope, it can be used, but not modified (unless it is a list etc. and methods like .append are used. But it can't be changed in place). As soon as a variable is modified in a function and there's no "global" keyword in the function, Python assumes that it is a local variable
18th May 2019, 9:58 AM
Anna
Anna - avatar
+ 1
Why does it bring issues? Just call it with the value, no matter from where it comes! f(imported_module.value) f(int(input()) for n in some_list: f(n)
18th May 2019, 9:55 AM
HonFu
HonFu - avatar
+ 1
Teddy Okello Yes, but why would you need so many global variables ? Usually the global keyword isn't used a lot because most of times variables are defined inside a class for specific use. But yes, you need to. Can you show me a code where you used many global variables ?
18th May 2019, 10:09 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
HonFu , see what you mean now. When creating the function I have to pass the global variable as an argument and pass it again when I call the function. Thanks. That helped.
18th May 2019, 10:32 AM
Teddy Okello
Teddy Okello - avatar
- 1
Aymane Boukrouh I don't want to use global because it is not professional and brings confusion. HonFu ,please elaborate your last point further .
18th May 2019, 9:39 AM
Teddy Okello
Teddy Okello - avatar
- 1
HonFu ,doing it the second way will require me to enter a value for n for every function and class that requires it. If n is provided by the user or an imported module and needs to be accessed by the more than one code block, it can bring issues.
18th May 2019, 9:51 AM
Teddy Okello
Teddy Okello - avatar
- 1
Anna and Aymane Boukrouh maybe I was wrong about the professional thing sorry about that. Can a variable defined in the global space be modifed locally without the use of global key word?
18th May 2019, 9:56 AM
Teddy Okello
Teddy Okello - avatar
- 1
So basically I have to use the the global keyword for all my global variables. What if I have a large number of global variables that need to be called and modified in different class methods and functions?
18th May 2019, 10:04 AM
Teddy Okello
Teddy Okello - avatar
- 1
Anna I don't have the code right now, but I feel I have to learn more OOP because this problem may stem from my poor understanding of how variables are handled by classes. It seems I need to do more research. For example, the __init__ method. I thought that is where all class variables are defined and global variables are introduced locally.
18th May 2019, 10:20 AM
Teddy Okello
Teddy Okello - avatar