Created
September 14, 2015 14:14
-
-
Save odra/ea36614c82ccf5b7f59a to your computer and use it in GitHub Desktop.
python decorators
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
# -*- coding: utf-8 -*- | |
def decorator_without_params(fn): | |
def wrapper(*args, **kwargs): | |
fn_result = fn(*args, **kwargs) | |
return '%s from decorator without params' % fn_result | |
return wrapper | |
def decorator_with_params(name): | |
def decorator(fn): | |
def wrapper(*args, **kwargs): | |
fn_result = fn(*args, **kwargs) | |
return '%s from decorator with param: %s' % (fn_result, name) | |
return wrapper | |
return decorator | |
@decorator_without_params | |
def func(ctx): | |
return 'hello %s' % ctx | |
@decorator_with_params('a') | |
def func1(ctx): | |
return 'hello %s' % ctx | |
if __name__ == '__main__': | |
print func('world') | |
print func1('world') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment