Last active
June 15, 2019 09:44
-
-
Save mikebohdan/ec7e9ced1b5c0ae8af2d38f1c3ff211f to your computer and use it in GitHub Desktop.
TCO for python 3.7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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