I was considering a similar case like this question I've asked: Set Timeout Function CallBack static variables
But, in python. I have tried to do some searching but can't seem to figure out how to find it.
I was considering if I had a scenario I needed to perform an operation in the future, and needed to wait (or a timer for example), but the operation may require data at that moment in time (when I called the function that is to be calling the callback in the future).
In Javascript I understand there is the bind operation, or using closures.
I was wondering if people actually do this in python, and if so what are/is the common conventions regarding it?
I'm thinking of code something like this:
import threading
import time
x = "ABCD"
class Typewriter(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
time.sleep(5)
print x
# make it type!
def foo(q):
q
def bar():
typer = Typewriter()
typer.start()
s = bar()
foo(s)
x = "DEF"
I can't really pass a value into the foo(s), (I tried foo(s(x)) and I get the same result).
Is there something similar to python? I could say do
foo(s(q).bind(x))
If so what are the normal conventions? Are there more than one way to skin a cat here?
(I know I know technically I can use x in the Typewriter class initialization and that would happen before the variable x gets changed, however I couldn't think of a better way to express the equivalent function as in the first question). My main goal here is to be able to pass in the variable to be used in the future, and not use it more or less indirectly.