Skip to content Skip to sidebar Skip to footer

Accessing A Function's Variable From Another Function

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 ( Ign

Solution 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:

classKCounter(object):

    def__init__(self):
        self.k = 0defprint_k(self):
        whileTrue:
            if self.k % 2 == 1:
                print(self.k, "is even.")
            time.sleep(2)

    defmain(self):
        self.k = 1while self.k != 200:
            self.k += 1print 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.

Post a Comment for "Accessing A Function's Variable From Another Function"