- decoratorの復習
- wrapsってなに?
- その効果
Last active
July 24, 2017 02:25
-
-
Save ksomemo/58b1a8339bc7a0c12aaf to your computer and use it in GitHub Desktop.
decorator メモ
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
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