0

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

cryptiblinux
  • 29
  • 3
  • 12
  • 3
    That code wouldn't be any more valid in Java than it is in Python. You can't have two returns after each other in a function; but if you want to use the value from a function, you need to *call* it, and use the return value. – Daniel Roseman Apr 27 '16 at 17:40

4 Answers4

1

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
Keiwan
  • 8,031
  • 5
  • 36
  • 49
1

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)): ....
Pythonista
  • 11,377
  • 2
  • 31
  • 50
1

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()
Community
  • 1
  • 1
Igor
  • 1,212
  • 12
  • 23
  • Thanks @Igor. The OOP example works fine. Could you explain where the indent fault is with my updated code? – cryptiblinux Apr 27 '16 at 20:09
  • The two methods of the class you are defining should be indented under the class name. See how in my example both method definitions appear indented within the ``Sample`` class definition? In your code sample you are defining a method and class on the same indentation level. When I try to run your code sample, I get indentation error and not the error you have. It could be because I am using python3 instead of python2. Unless you have a good reason to use 2 I would highly recommend 3. – Igor Apr 27 '16 at 20:15
0

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