Created
May 19, 2013 08:00
-
-
Save fabiocerqueira/5607017 to your computer and use it in GitHub Desktop.
Brincando com decorators no Python 2, com e sem uso de partial
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
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
from functools import partial, wraps | |
def debug_old(prefix=''): | |
def debug(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
print(prefix + func.__name__) | |
return func(*args, **kwargs) | |
return wrapper | |
return debug | |
def debug(func=None, prefix=''): | |
if func is None: | |
return partial(debug, prefix=prefix) | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
print(prefix + func.__name__) | |
return func(*args, **kwargs) | |
return wrapper | |
@debug_old() | |
def add(a, b): | |
return a + b | |
@debug_old(prefix='>>> ') | |
def sub(a, b): | |
return a - b | |
@debug | |
def mul(a, b): | |
return a * b | |
@debug(prefix='>>> ') | |
def div(a, b): | |
return a / b | |
if __name__ == '__main__': | |
print(add(10, 5)) | |
print(sub(10, 5)) | |
print(mul(10, 5)) | |
print(div(10, 5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment