Skip to content

Instantly share code, notes, and snippets.

@dmiro
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save dmiro/7192de69da098b2d2c95 to your computer and use it in GitHub Desktop.

Select an option

Save dmiro/7192de69da098b2d2c95 to your computer and use it in GitHub Desktop.
Python simple decorator
"""
Python simple decorator example with function
"""
import functools
def function_wrapper(wrapped): # function closure
@functools.wraps(wrapped)
def _wrapper(*args, **kwargs):
print "enter wrapper"
return wrapped(*args, **kwargs)
print "exit wrapper"
return _wrapper
@function_wrapper
def function():
pass
# test
print(function.__name__) # special atributes __name__ & __doc__ were added
function()
function()
# result
"""
function
enter wrapper
enter wrapper
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment