函数接口的Python库?

2024-05-14 20:42:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道是否有一种现有的Python库/技术来实现函数interfaces/“contracts”。像ABC一样的东西,但是函数。在

例如,一个使用合成语法的示例:

@implements(i_state_updater)
def my_update(position, state, forces):
    ... 
    return new_position, new_state

在哪里

^{pr2}$

所以当我把这个函数作为参数传递时,我可以验证它,例如

def compute_trajectory(update_func, n_steps, initial_state):
    """
    :param update_func: An i_state_updater
    ...
    """
    assert update_func.implements(i_state_updater)

Python现有的abc模块做了类似的事情,但只针对类。有没有功能的等价物?在


Tags: 函数示例newdef语法positionupdateinterfaces

热门问题