Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
added 5 characters in body
Source Link
Peter

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.obeysimplements(i_state_updater)

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

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.obeys(i_state_updater)

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

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?

Source Link
Peter

Python Library for Function Interfaces?

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.obeys(i_state_updater)

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

lang-py