Skip to content

Instantly share code, notes, and snippets.

@odra
Created September 14, 2015 14:14
Show Gist options
  • Save odra/ea36614c82ccf5b7f59a to your computer and use it in GitHub Desktop.
Save odra/ea36614c82ccf5b7f59a to your computer and use it in GitHub Desktop.
python decorators
# -*- 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