Skip to content

Instantly share code, notes, and snippets.

@masahitojp
Created June 26, 2012 09:30
Show Gist options
  • Select an option

  • Save masahitojp/2994653 to your computer and use it in GitHub Desktop.

Select an option

Save masahitojp/2994653 to your computer and use it in GitHub Desktop.
>>> 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
([],)
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