As pointed out by Mark Meyer, the variables are scoped to functions and there is no way for variables inside test
to exist if you do not call the function test()
function.
In case you do run test()
and don't want to return and pass variables to other functions, you would need to define the variables as global
, so they are not confined to the function scope.
def test():
global number_1
global number_2
number_1=10
number_2=20
def number_function():
print(number_1)
print(number_2)
test()
number_function()
Out[1]:
10
20
Another way to share variables across functions is to simply use classes:
Class yourEnv(object):
def __init__(self):
self.number_1 = 10
self.number_2 = 20
def number_function(self):
print(self.number_1)
print(self.number_2)
yourEnv().number_function()
Out[2]
10
20