4

I'm wondering if there's an existing Python libary/technique for enforcing function interfaces/"contracts". Something like ABC but for functions.

E.g. An example with made-up syntax:

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

Where

@declare_interface
def i_state_updater(position, state, forces):
    """
    :param position: ...
    :returns: ....
    """

So that when I pass this function as an argument I can verify it, eg

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

Python's existing abc module does something like this, but only for classes. Is there an equivalent out there for functions?

0

1 Answer 1

2

PyContract seems like a library that you would want to look into.

See https://pypi.python.org/pypi/PyContracts

See also

Sign up to request clarification or add additional context in comments.

1 Comment

Yes! That's exactly what I was looking for - somehow my searching hasn't found it before. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.