Skip to content

Instantly share code, notes, and snippets.

@ksomemo
Last active July 24, 2017 02:25
Show Gist options
  • Save ksomemo/58b1a8339bc7a0c12aaf to your computer and use it in GitHub Desktop.
Save ksomemo/58b1a8339bc7a0c12aaf to your computer and use it in GitHub Desktop.
decorator メモ
  • decoratorの復習
  • wrapsってなに?
  • その効果
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
from functools import (
wraps,
update_wrapper
)
# http://docs.python.jp/3.5/library/functools.html#functools.update_wrapper
def decorator_nowraps(f):
def wrapper(*args, **kwds):
print('decorated func nowarps')
return f(*args, **kwds)
return wrapper
def decorator_wraps(f):
@wraps(f)
def wrapper(*args, **kwds):
print('decorated fun wraps')
return f(*args, **kwds)
return wrapper
def decorator_update_wrapper(f):
def wrapper(*args, **kwds):
print('decorated func wraps and update')
return f(*args, **kwds)
return update_wrapper(wrapper, wrapped=f)
@decorator_nowraps
def example1():
"""Docstr ex1"""
print('example1')
@decorator_wraps
def example2():
"""Docstr ex2"""
print('ex2')
@decorator_update_wrapper
def example3():
"""Docstr ex3"""
print('ex3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment