Suppose I have a class A
that has a contains_variable
method that contains multiple variables. What I want to achieve here is declaring all these variables global
so that they could be used outside their current local scope.
What I don't want is to make these variables class variables or define them outside this contains_variable
method since it contains self.foo
and other such variables that are used in other classes and files and defining them outside the method would mean changing them everywhere else as well which would be cumbersome.
class A:
def contains_variables(self, foo):
self.foo = None
var1 = 1
var2 = 2
var3 = 3
# upto let's say var 28
var 28 = 28
def add(self):
print('var1 + var28 = ', var1 + var28)
I realized defining other class for these variables must be a good option but then again it would mean to change every variable at every place it was used.
class globalVariables:
foo = None
var1 = 1
# upto var28
var28 = 28
class A(globalVariables):
print('var1 + var28 = ', globalVariables.var1 + globalVariables28)
Prepending every variable with globalVariables
just seems unproductive.