Object Oriented Programming and making a
a member variable is absolutely the best solution here.
But sometimes you have codes that are not OO, think of a flask application for example when you have multiple endpoints that you'd like to share some value. In this case, a shared or global variable is the way to go. Meaning defining the varibale outside the scope of all the methods so it could be accessed anywhere.
Now, if the value of your variable never changes, you sould use uppercase letters for naming, to mark it as final and in a sense global [similar to final static variables in Java].
A = ['A','X','R','N','L']
But if the value does change, first, the name should be lowercase a = ['A','X','R','N','L']
. Second, you'd want to limit the places where the value can change, ideally to only one method, and there you can use the global
keyword to
change the value
a = ['A','X','R','N','L']
def fun1[string,vect]:
global a
a.append['W']
out = []
for letter in vect:
out. append[string+letter]
return out
def fun2[number,vect]:
out = []
for letter in vect:
out.append[str[number]+letter]
return out
x = fun1['Hello ',a]
y = fun2[2,a]
If you find yourself changing the value of a
in multiple places in your code, a shared/global variable is probably not what you're looking for and instead you should just pass the value around as a parameter.
Learn how to create global variables that can be shared between files/modules in Python.
Global variables are used in programming to share memory between different sections of an application, this allows for a range of functionality such as keeping track of resource use between classes, storing statistics about an application and much more. In Python, they are mainly used to share values between classes and functions.
Understanding global variables in Python
In Python, a conventional global variable is only used to share a value within classes and functions. A variable- once declared as a global within a function or class can then be modified within the segment.
num = 1 def increment[]: global num num += 1
The above code would allow the variable 'num' to be used within that function, whereas if you were to omit the global statement, it would be undefined.
If you were to attempt to use this logic to declare a global variable from another class however, it would not work as it would in other languages such as Java or C++.
For example, if we had one class with a variable that needed to be accessed from within another imported class, there would be no way for us to access this variable using the global keyword:
main.py
import test num = 1 test.increment[]
test.py
def increment[]: global num num += 1
As you can see above, even though we try to access 'num' as a global within the increment[] function after it has been given a value in main.py, it will still not be defined and will result in an error.
Sharing global variables between files/modules in Python
Even though the global keyword is conventionally only used to access variables from within the same module, there are ways to use it in order to share variables between files. For example, we could take influence from PHP and create something similar to the "superglobals" variables.
To do this, you can create a new module specifically for storing all the global variables your application might need. For this you can create a function that will initialize any of these globals with a default value, you only need to call this function once from your main class, then you can import the globals file from any other class and use those globals as needed.
For example, let's create a function similar to the example above that will increment a global variable. For this we'll need 3 classes to prove the concept; a main class, a class containing our increment function and a class containing the globals.
globals.py
def initialize[]: global num num = 1
main.py
import globals import test if __name__ == "__main__": globals.initialize[] print[ globals.num ] test.increment[] print[ globals.num ]
test.py
import globals def increment[]: globals.num += 1
When we run main.py the output will be:
1 2
As you can see, once we've initialized the global variable in globals.py, we can then access 'num' as a property from any other module within the application and it will retain its value.
If you're having trouble understanding how this works, feel free to leave a comment below!