是否有 python 库可以让您在函数内部调用函数而不创建递归堆栈

Is there a python libary that lets you call a function inside a function wtihout creating a recursive stack

我正在尝试从一种状态切换到另一种状态。每个状态由一个函数表示。我想处理函数内部切换的逻辑,而不是调用函数的主循环。是否有允许这种模式的 python 库。 例如:

def c(count):
    if count<10:
        a(count+1)
        print("dont' keep track of post-order printing")
    else:
        print("when count is reached, program should terminate")
def b(count):
    c(count):
def a(count):
    b(count)
a()

这只是一个示例,我正在尝试使用有限状态模式,其中逻辑在转换 on_enter 方法中。基本上,机器会自动从一种状态转换到另一种状态,而无需使用 main_loop。

有fn.tcorhttps://github.com/kachayev/fn.py/blob/master/README.rst#trampolines-decorator。但是这个库似乎没有维护..你可以在 Python 中寻找尾部优化......你可能会找到一些东西..

希望能符合您的期望

def c(count):
    if count<10:
        print("don't keep track of post-order printing")
        c(count+1)
    else:
        print("when count is reached, program should terminate")

输出:

>>> c(1)
don't keep track of post-order printing
don't keep track of post-order printing
don't keep track of post-order printing
don't keep track of post-order printing
don't keep track of post-order printing
don't keep track of post-order printing
don't keep track of post-order printing
don't keep track of post-order printing
don't keep track of post-order printing
when count is reached, program should terminate

transitions 具有有序过渡以及 queued 过渡,这可能正是您正在寻找的。基本技巧是在处理完同一事件时调用同一事件(finalize)。即使转换不成功(并非所有 conditions 都返回 True),也会处理传递给 finalize 的回调。使用 queued 转换不会立即处理事件,而是在当前处理的事件之后立即处理,这将防止大量递归。

from transitions import Machine
import time


class Model(object):

    # initialise counter and counter limit
    def __init__(self):
        self.counter = 0
        self.limit = 5

    # will be called in every cycle and increase the counter
    def increase_counter(self):
        self.counter += 1
        print("Counter increased to ", self.counter)
        time.sleep(0.5)

    # will be called whenever a new state has been entered
    def reset_counter(self):
        self.counter = 0
        print("Counter reset; Current state is ", model.state)

    # this function returns whether the limit has already been reached
    def limit_reached(self):
        return self.counter >= self.limit

# initialising the previously defined model
model = Model()
# creating some state names
states = ['A', 'B', 'C', 'D']

# configuring the state machine:
# pass the model (for callbacks), pass the state names,
# disable auto_transitions since we will not need them
# set the initial state to 'A' and call a (currently) undefined
# model function 'next_state' after EVERY triggered event.
# 'queued' means that every transition is finished before the next event is handled
machine = Machine(model, states=states, auto_transitions=False,
                  queued=True, initial='A', finalize_event='next_state')

# now add ordered transitions: 
# Depending on the order of the passed state names,
# create transitions from each state 'n' to state 'n+1' called 'next_state'.
# 'prepare' each transition attempt by increasing the counter
# afterwards check the 'conditions' (is the counter limit reached)
# if all callbacks in 'conditions' return True, the transition
# is conducted and callbacks in 'after' are processed (counter reset)
machine.add_ordered_transitions(prepare='increase_counter', conditions='limit_reached',
                                after='reset_counter', trigger='next_state')

# model will go into an infinite loop; can be triggered in a thread
model.next_state()

您可以尝试减少 increase_counter 中的睡眠计时器以检查您是否会遇到递归错误(您不应该)。如果您设置 queued=False 这是标准行为,您将或多或少立即遇到递归错误,因为所有机器触发器都将立即处理。