7

I have a small issue while calling multiple variables in python one function to another. Like I have to access the variable of xxx() variables in yyy(). Help me to do this.?

Example :

def xxx():
        a=10
        b=15
        c=20

def yyy():
        xxx()
        print a        ### value a from xxx()
        print b        ### value b from xxx()

yyy()
Ganeshgm7
  • 455
  • 1
  • 13
  • 20

6 Answers6

15

Return them from your first function and accept them in your second function. Example -

def xxx():
    a=10
    b=15
    c=20
    return a,b

def yyy():
    a,b = xxx()
    print a        ### value a from xxx()
    print b        ### value b from xxx()

yyy()
Sharon Dwilif K
  • 1,218
  • 9
  • 17
11

You can't. Variables created in a function are local to that function. So if you want function yyy to get the values of some variables defined in function xxx then you need to return them from xxx, as demonstrated in Sharon Dwilif K's answer. Note that the names of the variables in yyy are irrelevant; you could write:

def yyy():
    p, q = xxx()
    print p        ### value a from xxx()
    print q        ### value b from xxx()

and it would give the same output.

Alternatively, you could create a custom class. Briefly, a class is a collection of data together with functions that operate on that data. Functions of a class are called methods, the data items are known as attributes. Each method of a class can have its own local variables, but it can also access the attributes of the class. Eg

class MyClass(object):
    def xxx(self):
        self.a = 10
        self.b = 15
        self.c = 20

    def yyy(self):
        self.xxx()
        print self.a
        print self.b

#Create an instance of the class
obj = MyClass()

obj.yyy()

output

10
15

Please see the linked documentation for more information about classes in Python.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
2

Try this:

def xxx():
    a=10
    b=15
    c=20
    return a, b

def yyy():
    a, b = xxx()
    print a
    print b

yyy()

Or this:

def xxx():
    global a, b
    a=10
    b=15
    c=20

def yyy():
    print a
    print b

xxx()
yyy()

For more info, use help('return') and help('global'), or see this and this question.

And only use global when you need the variable can use at everywhere or you need the function return other things. Because modifying globals will breaks modularity.

Community
  • 1
  • 1
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
  • Please don't encourage people to modify globals. They are ok for constants, but modifying globals breaks modularity. And anyway, your globals code is wrong. You need `global a, b` in `yyy` and you need to call `xxx` somewhere. – PM 2Ring Sep 28 '15 at 12:38
  • @PM2Ring Frist, you're right, I forgot call `xxx()` :P. But `global a, b` in `yyy` is incorrect. It will raise a `NameError` when you call `yyy`. – Remi Guan Sep 28 '15 at 12:42
  • It will raise `NameError` when you attempt to print `a` if `a` hasn't been defined yet. But if you call `xxx`, either in `yyy` before the `print` statements, or in the global scope before you call `yyy`, then you won't get a `NameError`. – PM 2Ring Sep 28 '15 at 12:46
  • But yeah, you're right: you **don't** _need_ `global a, b` in `yyy` because `yyy` doesn't modify `a` or `b`. Sorry about that. As you might guess I **rarely** use globals. :) – PM 2Ring Sep 28 '15 at 12:48
  • @PM2Ring Okay :). And I have edited my answer to explain choose `global` or `return`. – Remi Guan Sep 28 '15 at 12:50
1

pickle.dump-workaround

In general, I'm working with a return statement if I need the variable for another function or a script.

But especially if I want to investigate why my code is failing with new data I'd like to analyze the parameters without rebuilding all the code.

My solution is just to store the file on disk.

def xxx():
    a=10

    tmp_dir = os.path.join(os.getcwd(),'tmp')
    if not os.path.exists(tmp_dir):        
        os.mkdir(tmp_dir)
    pickle.dump(a, open(os.path.join(tmp_dir,'a.pickle'), 'wb'))

    b=15
    c=20
    

Then you may load the parameter from where ever you want. In my case usually from a Jupyter-Notebook for the analyses.

def yyy():
    a = pickle.load(open(os.path.join(tmp_dir,'a.pickle'), 'rb'))
    print a        ### value a from xxx()
    print b 

Btw: this is for sure a construction I would only use to analyze errors.

Thomas R
  • 1,067
  • 11
  • 17
-1

The answers people gave above are correct. But from a design perspective this is not something correct. Method local variables are mean to be local. If at all there is something that needs to be shared across a file or some set of methods I suggest you use file level variables or constants.

Dhana
  • 505
  • 1
  • 8
  • 27
-2

Use the global keyword, like the following example:

global a=0
global b=0
global c=0

def xxx():
    a=10
    b=15
    c=20
    return a,b

def yyy():
    print a
    print b 
    xxx()
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • 1
    To format code, select it and click the `{}` button, which puts it in a code block. However, this code contains several errors. Please consider testing code before posting it on SO. – PM 2Ring Sep 28 '15 at 12:40