Created
August 10, 2017 13:31
-
-
Save vaclavcadek/71c4f606fe4bec3e57fd1f7c043a514a to your computer and use it in GitHub Desktop.
Example decorators in Python.
This file contains hidden or 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 logger(func): | |
def wrapped(*args, **kwargs): | |
print('Calling {} with args = {} and kwargs = {}'.format(func, args, kwargs)) | |
return func(*args, **kwargs) | |
return wrapped | |
@logger | |
def factorial(n): | |
if n == 1: | |
return 1 | |
else: | |
return n * factorial(n - 1) | |
@logger | |
def fibonacci(n): | |
if n < 2: | |
return n | |
else: | |
return fibonacci(n - 1) + fibonacci(n - 2) | |
print(fibonacci(5)) | |
print(factorial(5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment