Skip to content

Instantly share code, notes, and snippets.

@NotTheEconomist
Last active December 29, 2015 07:22
Show Gist options
  • Save NotTheEconomist/cca4fd565eac4d436d0b to your computer and use it in GitHub Desktop.
Save NotTheEconomist/cca4fd565eac4d436d0b to your computer and use it in GitHub Desktop.
def prune(f):
def wrapper(*args):
def wrapped(*args):
if wrapper.min:
print("wrapper.min is true")
else:
print("wrapper.min is false")
if random.random() > 0.5:
# recurse
return wrapped(*args)
else:
return True
wrapper.min = True
return wrapped(*args)
return wrapper
import random # after it's used in the inner function
# just to show off the difference between definition and execution
@prune
def foo():
# We never called this function, so who cares
pass
foo()
>>> foo()
wrapper.min is true
wrapper.min is true
wrapper.min is true
wrapper.min is true
True
>>> foo()
wrapper.min is true
True
>>> foo()
wrapper.min is true
True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment