Algebraic effects in Python?

I’m trying to map out what’s already been done with algebraic effects / effect handlers in Python, and I’d love pointers from people who know the space.

I’m aware generators (yield / send) and context managers can approximate one-shot, shallow handlers, but I’m more interested in fuller or more principled attempts — libraries, research experiments, or write-ups.

A few things I’m specifically curious about:

  • libraries that implement effects as a first-class abstraction
  • anything that tackles multi-shot continuations (greenlets? CPS transforms?)
  • how these compare to handlers in Koka / Eff / OCaml

Pointers, war stories, or “don’t bother, here’s why” all welcome.

I had to look that up. Thanks for the opportunity to learn.

On PyPI, some matches exist that you may want to explore: simple-eff and eff. Looking at their release history, the topic seems far from mainstream.

Most likely, some of the concepts you’re looking for are hard to even define (at least in a meaningful way) for Python, because the language is fundamentally so different from ones designed to focus purely on the functional paradigm.

Thanks for the replies! I eventually came across this:

I tried a small example that explores both states of a feature flag:

from aleff import create_handler, effect

feature_enabled = effect("feature_enabled")

def checkout_total():
    shipping = 0 if feature_enabled("free_shipping") else 500
    return 3_000 + shipping

explore_both = create_handler(feature_enabled)

@explore_both.on(feature_enabled)
def _(resume, flag):
    return {"off": resume(False), "on": resume(True)}

print(explore_both(checkout_total))
# {'off': 3500, 'on': 3000}

This is the kind of multi-shot behavior I was looking for. Thanks again!