I'm trying to create a global variable, but from what I know you need to type "global" in every function.
This example is in Python 2.7:
a = 10
def change():
global a
a = 2
print a
def reverse():
global a
a = 10
print a
// Output:
// 2
// 4
I'm trying to re-create the code above without the use of global
for every variable in every function I am going to create. Is there a better way to do this?
To better explain this situation, I have a dictionary and I intend to use many different functions on it. The problem is, I don't really want to be using function(dict)
or do_this(dict)
each time, I prefer function()
or do_this()
. My thinking is that if dict
was global, I could solve this problem.
P.S Thank you for all the answers! Not exactly what I was looking for, but I appreciate the answers.