我想使用transitions,并且需要一个我在文档中找不到的相当微不足道的功能,并且想知道它是否已实现:
我想on_enter在某个状态上定义一个回调,但将一个参数传递给该回调。至少要知道我是从哪个州进入的。
从文档:
class Matter(object):
def say_hello(self): print("hello, new state!")
def say_goodbye(self): print("goodbye, old state!")
lump = Matter()
# Same states as above, but now we give StateA an exit callback
states = [
State(name='solid', on_exit=['say_goodbye']),
'liquid',
{ 'name': 'gas', 'on_exit': ['say_goodbye']}
]
machine = Machine(lump, states=states)
machine.add_transition('sublimate', 'solid', 'gas')
# Callbacks can also be added after initialization using
# the dynamically added on_enter_ and on_exit_ methods.
# Note that the initial call to add the callback is made
# on the Machine and not on the model.
machine.on_enter_gas('say_hello')
# Test out the callbacks...
machine.set_state('solid')
lump.sublimate()
>>> 'goodbye, old state!'
>>> 'hello, new state!'
我缺乏的是
def say_hello(self, param): print(f"hello, new state! here is your param: {param}")
这可以以某种方式很好地完成吗?
一个明显不好的解决方案是保留一个self._last_state论点并自己坚持下去。
我正在寻找内置的东西。