Created
February 13, 2010 11:49
-
-
Save banyan/303404 to your computer and use it in GitHub Desktop.
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
| class Klass(object): | |
| def decorator(original_func): | |
| def func(*args): | |
| return [x * 10 for x in original_func()] | |
| return func | |
| @decorator | |
| def decorated_func(): | |
| lists = [1, 2] | |
| return lists | |
| @decorator | |
| def decorated_func2(): | |
| lists = [10, 20] | |
| return lists | |
| klass = Klass() | |
| print klass.decorated_func() | |
| print klass.decorated_func2() | |
| # [10, 20] | |
| # [100, 200] | |
| # デコレータの定義を後ろにするとエラーが発生 | |
| class Klass(object): | |
| @decorator | |
| def decorated_func(): | |
| lists = [1, 2] | |
| return lists | |
| @decorator | |
| def decorated_func2(): | |
| lists = [10, 20] | |
| return lists | |
| def decorator(original_func): | |
| def func(*args): | |
| return [x * 10 for x in original_func()] | |
| return func | |
| klass = Klass() | |
| print klass.decorated_func() | |
| print klass.decorated_func2() | |
| # Traceback (most recent call last): | |
| # File "decorator.py", line 65, in <module> | |
| # class Klass(object): | |
| # File "decorator.py", line 67, in Klass | |
| # @decorator | |
| # NameError: name 'decorator' is not defined | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment