View on GitHub

Ann Molly Paul

learn -- code -- inspire

Writing effective functions

April 2, 2015

For readability

  1. Use variable arguments where possible e.g. def foo(*args)
  2. Use foo(*arg_list) when calling functionfoo which unpacks the list of arguments.
  3. Specify keyword arguments when changing default values in a function call rather than positional defaults.
    • def foo(a,b, c=10, d=True) So, when you call it foo(3,4, c=20,d=False).
  4. Python 3 syntactically can force keyword only arguments by using * as one of the arguments, enforcing all the arguments that follow to be keyword only
    • def foo(a,b,*, c=10, d=True) Python 3 syntax– force keyword-only args.

For performance:

Return generators rather than a list. Few advantages: