Created
June 26, 2012 09:30
-
-
Save masahitojp/2994653 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
| >>> def test(a=[]): | |
| ... a.append('A') | |
| ... return a | |
| ... | |
| >>> import copy | |
| >>> z1= copy.copy(test.func_defaults) | |
| >>> z2= copy.deepcopy(test.func_defaults) | |
| >>> z1 == z2 | |
| True | |
| >>> test() | |
| ['A'] | |
| >>> z1 == z2 | |
| False | |
| >>> z1 | |
| (['A'],) | |
| >>> z2 | |
| ([],) |
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
| import functools | |
| import copy | |
| def clear_func_default(f): | |
| # 関数のデフォルト値が上書きされることを防ぐためのデコレータ | |
| @functools.wraps(f) | |
| def wrapper(*args, **kward): | |
| temp = copy.deepcopy(f.func_defaults) | |
| result = f(*args, **kward) | |
| f.func_defaults = temp | |
| return result | |
| return wrapper | |
| @clear_func_default | |
| def bar(a=[]): | |
| """ | |
| >>> bar() | |
| ['A'] | |
| >>> bar() | |
| ['A'] | |
| >>> bar(['B']) | |
| ['B', 'A'] | |
| """ | |
| a.append("A") | |
| return a | |
| def _test(): | |
| import doctest | |
| doctest.testmod() | |
| if __name__ == "__main__": | |
| _test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment