Accessing a function's variable from another function

后端 未结 1 527
我寻月下人不归
我寻月下人不归 2021-01-27 09:08
from threading import Thread
import time

def print_k():
    while true:
        if main.k % 2 == 1: # ditto
            print(main.k, \"is even.\") # <-- my problem          


        
相关标签:
1条回答
  • 2021-01-27 09:50

    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.

    0 讨论(0)
提交回复
热议问题