I'm new to python coming from a Java background and was wondering how to access variables from one function to another in Python. From this code:
def method1():
var1 = randomArray[::2]
var2 = randomArray[1::2]
return var1
return var2
def method2():
for i in range (len(var1)):
print i
I would get error message:
NameError: global name 'var1' is not defined
Forgive the code, I'm just providing an example. Any advice on how to use var1 in method 2 would be of great help. Thanks.
Edit:
class Sample:
def method1():
randomArray = [1,2,3,4,5,6,7,8,9,10]
var1 = randomArray[::2]
var2 = randomArray[1::2]
return var1, var2
def method2():
var1, var2 = method1()
for i in range (len(var1)):
print i
Still an error with method1() being called in method2().