I'm trying to figure out, which Python idiom has been used at next lines.
state = state0
while state:
state = state()
I'm confused why here is state0 instead of state0()? And what this line
state = state()
is doing? Why there isn't state0() ?
from random import random
from time import sleep
def state0():
print("state0")
# delay and decision path to simulate some application logic
sleep(.5)
if random()>.5:
return state1
else:
return state2
def state1():
print("state1")
# delay and decision path to simulate some application logic
sleep(.5)
if random()>.5:
return state0
else:
return state2
def state2():
print("state2")
# delay and decision path to simulate some application logic
sleep(.5)
if random()>.5:
return state0
else:
return None
state=state0 # initial state
while state:
state=state() # launch state machine
print("Done with states")