The three rules of Python Function Arguments

1. Default values come last

  • rule: argument with default values must come after those without default values
def printMessage(message="Hello World"): print(message)>>> printMessage()
Hello World

2. Default values only on immutables

  • rule: do not use mutable default values; use only immutable default values
  1. ints
  • lots of newcomers to Python are affected by this
  • remember that def itself is a statement executed at runtime
  • the fix is to use immutable

3. Keyword parameters come after positional

  • rule: all keyword parameters must be specified after positional parameters
  • and it is the same in python functions

Technical Support Engineer of a Data Collection Platform (Snowplow Analytics)