Skip to content

Instantly share code, notes, and snippets.

@rockneurotiko
Created September 17, 2014 06:18
Show Gist options
  • Save rockneurotiko/34f9197cca00c218d701 to your computer and use it in GitHub Desktop.
Save rockneurotiko/34f9197cca00c218d701 to your computer and use it in GitHub Desktop.
tail_rec_print
import functools
def tail_call(func):
def _optimize_partial(*args, **kwargs):
old_reference = func.__globals__[func.__name__]
func.__globals__[func.__name__] = functools.partial(functools.partial, func)
to_execute = functools.partial(func, *args, **kwargs)
while isinstance(to_execute, functools.partial):
to_execute = to_execute()
func.__globals__[func.__name__] = old_reference
return to_execute
functools.update_wrapper(_optimize_partial, func)
return _optimize_partial
@tail_call
def t(n = 1):
print("%s - Hello" % n)
return t(n+1) if n < 1024 else 0
t()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment