Created
October 2, 2017 15:33
-
-
Save tomncooper/68f3ec51e513c4270084bdc12bc38de3 to your computer and use it in GitHub Desktop.
Decorator examples
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
class after5(object): | |
def __init__(self, func): | |
self.func = func | |
self.counter = 0 | |
def __call__(self): | |
if self.counter > 4: | |
self.func() | |
self.counter += 1 | |
if __name__ == "__main__": | |
@after5 | |
def doit(): | |
print("Yo!") | |
# Theses should print nothing | |
doit() | |
doit() | |
doit() | |
doit() | |
doit() | |
# This should print something | |
doit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment