_functools.py 411 B

1234567891011121314151617181920
  1. import functools
  2. # from jaraco.functools 3.5
  3. def pass_none(func):
  4. """
  5. Wrap func so it's not called if its first param is None
  6. >>> print_text = pass_none(print)
  7. >>> print_text('text')
  8. text
  9. >>> print_text(None)
  10. """
  11. @functools.wraps(func)
  12. def wrapper(param, *args, **kwargs):
  13. if param is not None:
  14. return func(param, *args, **kwargs)
  15. return wrapper