简体   繁体   中英

How can I share a variable between functions in Python?

I have two functions, fun1 and fun2 , which take as inputs a string and a number, respectively. They also both get the same variable, a , as input. This is the code:

a = ['A','X','R','N','L']

def fun1(string,vect):
    out = []
    for letter in vect:
        out. append(string+letter)
    return out

def fun2(number,vect):
    out = []
    for letter in vect:
        out.append(str(number)+letter)
    return out

x = fun1('Hello ',a)
y = fun2(2,a)

The functions perform some nonsense operations. My goal would be to rewrite the code in such a way that the variable a is shared between the functions, so that they do not take it as input anymore.

One way to remove variable a as input would be by defining it within the functions themselves, but unfortunately that is not very elegant. What is a possible way to reach my goal?

The functions should operate in the same way, but the input arguments should only be the string and the number ( fun1(string) , fun2(number) ).

Object-oriented programming helps here:

class MyClass(object):
    def __init__(self):
        self.a = ['A','X','R','N','L']  # Shared instance member :D

    def fun1(self, string):
        out = []
        for letter in self.a:
            out.append(string+letter)
        return out

    def fun2(self, number):
        out = []
        for letter in self.a:
            out.append(str(number)+letter)
        return out

a = MyClass()
x = a.fun1('Hello ')
y = a.fun2(2)

An alternative to using classes: You can use the global keyword to use variables that lie outside the function.

a = 5
def func():
    global a
    return a+1

print (func())

This will print 6.

But global variables should be avoided as much as possible.

Since a<\/code> is defined outside the function scope and before<\/strong> the functions are defined, you do not need to feed it as an argument. You can simply use a<\/code> .

a = ['A','X','R','N','L']

def fun1(string):
    out = []
    for letter in a:
        out.append(string+letter)
    return out

def fun2(number):
    out = []
    for letter in a:
        out.append(str(number)+letter)
    return out

x = fun1('Hello ')
y = fun2(2)

This can be easily achieved using the global<\/code> keyword. That makes the a<\/code> variable available in the whole file. However, the global variables should be avoided as much, because every function has access to these, it becomes increasingly hard to figure out which functions actually read and write these variables.

a = ['A','X','R','N','L']

def fun1(string):
    out = []
    for letter in a:
        out. append(string+letter)
    return out

def fun2(number):
    out = []
    for letter in a:
        out.append(str(number)+letter)
    return out

x = fun1('Hello ')
y = fun2(2,a)

Object Oriented Programming<\/a> and making a<\/code> a member variable is absolutely the best solution here.

In this case, a shared or global variable is the way to go. Meaning defining the varibale outside the scope of all the methods so it could be accessed anywhere.

Second, you'd want to limit the places where the value can change, ideally to only one method, and there you can use the global<\/code> keyword to change the value

a = ['A','X','R','N','L']

def fun1(string,vect):
    global a
    a.append('W')

    out = []
    for letter in vect:
        out. append(string+letter)
    return out

def fun2(number,vect):
    out = []
    for letter in vect:
        out.append(str(number)+letter)
    return out

x = fun1('Hello ',a)
y = fun2(2,a)

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