简体   繁体   中英

Access a Python variable which is inside a function, in another function

Let's say I have a function:

func1():
    do something
    a,b = somevalues,somevalues

Now let's say I have another function:

func2():
    do something

I would now like to now use a,b in func2() which were calculated inside func1() .

I tried using

func1():
    do something
    a,b = somevalues, somevalues
    func1.a = somevalues
    func1.b = somevalues

func1()
func1.a

But using this, I have to each time run func1() before using a,b .

func1() also creates some plots along with calculating a,b .

So is it possible to utilise a,b in func2() without calling func1() ?

This may be what you want. Also, I suggest you work through an introductory Python tutorial.

func1():
    #do something
    a,b = somevalues, somevalues
    return a, b

func2(a, b):
    #do something on a and b

a, b = func1()
func2(a, b)

The answer by robert is probably the easiest way to do what your want. Here is another:

class class1():
    a,b = somevalues, somevalues
    @staticmethod
    def func1():
        do something

func2(class1.a, class1.b)
class1.func1()

The reason this works and your way doesn't work has to do with the way Python treats functions and classes.

The body of a function is not executed until it is called the first time. So when you have:

def func1():
    func1.a = somevalue

...you cannot access func1.a until func1 has been called at least once, as you have already discovered.

However, for classes, the body of the class runs when the code is compiled. So that when you do:

class example:
    a = somevalue

...you are able to access example.a immediately.

EDIT:

Answering the question in the comments: access func1 , a , or b as shown above using the class itself (in Python, classes ARE objects, just like any other object):

class1.a
class1.func1()

You could also make a shortcut for yourself:

func1 = class1.func1
func1()

Another way to do things- so that you could have different versions of a and b - be to make a and b instance attributes instead of class attributes.

class class1:
    def __init__(self, a, b):
        self.a, self.b = a, b
    @staticmethod
    def func1():
        dosomething

obj1 = class1(somevalue1A, somevalue1B)
obj2 = class1(somevalue2A, somevalue2B)
func2(obj1.a, obj1.b)
func2(obj2.a, obj2.b)
obj1.func1()
obj2.func1()
class1.func1()

The last three lines all call the same function, because obj1.func1 , obj2.func1 , and class1.func1 all point to the same method of class1 . Note that the reason you can call func1 from both the class ( class1 ) and the instances ( obj1 , obj2 ) of the class is that func1 is a static method.

func1():
    do something
    a,b = somevalues,somevalues
    return a, b

x, y = func1()

Now you can use x and y everywhere without callin func1() every time. For example in func2() this way:

def func2(a, b):
    do something

Then you call func2() with x and y respectivly:

func2(x, y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM