I want to use transitions, and need a rather trivial feature I could not find in the docs, and was wondering if it was implemented:
I want to define a on_enter
callback on some state, but pass a parameter to that callback. At least to know from which state I am entering the state.
From the docs:
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!'
What I lack is
def say_hello(self, param): print(f"hello, new state! here is your param: {param}")
Can this be done nicely somehow?
An obvious bad solution would be to keep a self._last_state
argument and maintain that myself.
I am looking for something built-in.