Accessing a function's variable from another f

2019-03-04 14:22发布

问题:

from threading import Thread
import time

def print_k():
    while true:
        if main.k % 2 == 1: # ditto
            print(main.k, "is even.") # <-- my problem is HERE ( Ignore all the other stuff )
        time.sleep(2)

def main():
    k = 1
    while k != 200:
        k += 1
        print k
        time.sleep(0.5)

if __name__ == '__main__':
    Thread(target=print_k).start()
    Thread(target=main).start()

in this script (example only, ignore all realistic functionality) I am trying to run main(), which adds up to 200 and prints it, and in print_k, i am printing main's variable, k. I have an exception raised, unsurprisingly, and am wondering how i can access a separate function's variable from a different function (they are both running at the same time, by the way, hence the Threading module.)

回答1:

You can't print main's variable k. The whole point of local variables is that they're local. It doesn't matter whether they're running at the same time or not; they each have their own separate local environment. (In fact, if you call main 60 times, each of those 60 calls has its own local environment.)

But there are a number of things you can do.

The simplest, but generally worst, is to use global variables instead of local variables. Just add global k to the top of the main function, add some start value for k at the top level (before either thread starts), and you can now access the same global variable inside print_k.

Bundling the shared state and functions up together in a class, where both functions become methods that can access self.k, is a better solution. Passing in some kind of mutable "holder" to both main and print_k is also a better solution. Redesigning your app around explicit message passing (e.g., on a Queue.Queue) is even better.

I'll show how to do it with a class:

class KCounter(object):

    def __init__(self):
        self.k = 0

    def print_k(self):
        while True:
            if self.k % 2 == 1:
                print(self.k, "is even.")
            time.sleep(2)

    def main(self):
        self.k = 1
        while self.k != 200:
            self.k += 1
            print self.k
            time.sleep(0.5)

if __name__ == '__main__':
    kcounter = KCounter()
    Thread(target=kcounter.print_k).start()
    Thread(target=kcounter.main).start()

Now, because we're using self.k, an attribute of the KCounter instance, instead of k, a local variable, both methods see the same variable.