1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| from functools import partial
def thread():
def action(m):
nonlocal h
if m == "toto":
func=partial(do_something_1, 1, 2, 3)
h.append((m, func))
func()
elif m == "titi":
func=partial(do_something_2, "x", "y")
h.append((m, func))
func()
elif m == "history":
print(tuple("%d: %s" % (i, x[0]) for (i, x) in enumerate(h)))
elif isinstance(m, int):
(label, func)=h[m]
print("repeat %d (%s)" % (m, label))
func()
# if
# action()
h=list()
return action
# thread()
def do_something_1(arg1, arg2, arg3):
'''do something'''
print(arg1, arg2, arg3)
return True
def do_something_2(arg1, arg2):
'''do something else'''
print(arg1, arg2)
return False
audio=thread()
audio("toto")
audio("titi")
audio("titi")
audio("toto")
audio("history")
audio(-1)
audio(2) |
Partager