cryptiblinux
cryptiblinux

Reputation: 27

Accessing variables in other functions in Python

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().

Upvotes: 0

Views: 96

Answers (4)

Igor
Igor

Reputation: 1292

In Python the return statement exits the function. So your second return var2 line of code will never be reached. The best way to get this done is to assign your return value to a variable when you call the function. Here is an example:

def method1():
    var1 = randomArray[::2]

    return var1


def method2():
    var1 = method1()
    for i in range (len(var1)):
        print i

You can also return multiple variables but you have to package them, for example as a tuple or a list.

Tuple Example:

def method1():
    var1 = randomArray[::2]
    var2 = randomArray[1::2]

    return (var1, var2)


def method2():
    var1, var2 = method1()
    for i in range (len(var1)):
        print i

    for i in range (len(var2)):
        print i

List Example:

def method1():
    var1 = randomArray[::2]
    var2 = randomArray[1::2]

    return [var1, var2]


def method2():
    varList = method1()
    for i in range (len(varList[0])):
        print i

    for i in range (len(varList[1])):
        print i

Here is an OOP example based on your updated code:

class Sample:

    def method1(self):
        randomArray = [1,2,3,4,5,6,7,8,9,10]
        var1 = randomArray[::2]
        var2 = randomArray[1::2]

        return var1, var2

    def method2(self):
        var1, var2 = self.method1()   
        print var1

MySample = Sample()
MySample.method2()

Upvotes: 1

Keiwan
Keiwan

Reputation: 8301

If you want to define your variables in your first function, you have to return them and save them when you call the function. Note that you can return multiple variables by separating them with a comma.

def method1():
    var1 = randomArray[::2]
    var2 = randomArray[1::2]

    return var1, var2

def method2():
    var1, var2 = method1()   

    for i in range (len(var1)):
        print i

Upvotes: 1

Dublin Anondson
Dublin Anondson

Reputation: 26

I'd make a separate method for var2 so both can be returned then call both functions in your other function. That way both can be accessed and you can avoid globals.

def method1():
    var1 = randomArray[::2]

    return var1

def method2():
    var2 = randomArray[1::2]

    return var2 

def method3():
    var1 = method1()
    var2 = method2()
    #var2 stuffmagic
    for i in range (len(var1)):
    print i

Upvotes: 0

Pythonista
Pythonista

Reputation: 11645

As the error says you have no global variable named var1. The variables in the function are local.

You could make the variable global which I wouldn't do.

Or, easier just return the values of var1 and var2 and use them in method2()

Also, your return is wrong. You should returning like so return var1, var2 if you want to return both.

e.g. -

def method1():
    var1 = randomArray[::2]
    var2 = randomArray[1::2]
    return var1, var2

def method2():

    var1, var2 = method1()
    #Do something with var2
    for item in var1:
       print item
    #or for i in range(len(var1)): ....

Upvotes: 1

Related Questions