Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. def func(items=[]):
  2. print items
  3.  
  4. def func(items=None):
  5. if items is None:
  6. items = []
  7. print items
  8.  
  9. my_copy = list(items)
  10.  
  11. from functools import wraps
  12. def defaultFactories(func):
  13. 'wraps function to use factories instead of values for defaults in call'
  14. defaults = func.func_defaults
  15. @wraps(func)
  16. def wrapped(*args,**kwargs):
  17. func.func_defaults = tuple(default() for default in defaults)
  18. return func(*args,**kwargs)
  19. return wrapped
  20.  
  21. def f1(n,b = []):
  22. b.append(n)
  23. if n == 1: return b
  24. else: return f1(n-1) + b
  25.  
  26. @defaultFactories
  27. def f2(n,b = list):
  28. b.append(n)
  29. if n == 1: return b
  30. else: return f2(n-1) + b
  31.  
  32. >>> f1(6)
  33. [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]
  34. >>> f2(6)
  35. [1, 2, 3, 4, 5, 6]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement