Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def func(items=[]):
- print items
- def func(items=None):
- if items is None:
- items = []
- print items
- my_copy = list(items)
- from functools import wraps
- def defaultFactories(func):
- 'wraps function to use factories instead of values for defaults in call'
- defaults = func.func_defaults
- @wraps(func)
- def wrapped(*args,**kwargs):
- func.func_defaults = tuple(default() for default in defaults)
- return func(*args,**kwargs)
- return wrapped
- def f1(n,b = []):
- b.append(n)
- if n == 1: return b
- else: return f1(n-1) + b
- @defaultFactories
- def f2(n,b = list):
- b.append(n)
- if n == 1: return b
- else: return f2(n-1) + b
- >>> f1(6)
- [6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1]
- >>> f2(6)
- [1, 2, 3, 4, 5, 6]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement