Last active
December 29, 2015 07:22
-
-
Save NotTheEconomist/cca4fd565eac4d436d0b to your computer and use it in GitHub Desktop.
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
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() |
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
>>> 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