Skip to content

Instantly share code, notes, and snippets.

@mikebohdan
Last active June 15, 2019 09:44
Show Gist options
  • Save mikebohdan/ec7e9ced1b5c0ae8af2d38f1c3ff211f to your computer and use it in GitHub Desktop.
Save mikebohdan/ec7e9ced1b5c0ae8af2d38f1c3ff211f to your computer and use it in GitHub Desktop.
TCO for python 3.7
from dataclasses import dataclass, field
from functools import wraps
@dataclass
class _TCOCall:
args: list = field(default_factory=list)
kwargs: dict = field(default_factory=dict)
def tco(f):
first_call = True
@wraps(f)
def wrapper(*args, **kwargs):
nonlocal first_call
r = _TCOCall(args, kwargs)
if first_call:
first_call = False
while isinstance(r, _TCOCall):
r = f(*r.args, **r.kwargs)
first_call = True
return r
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment