Default Arguments

Function definition with default values

     def spam(a,b, c=37, d="Guido"):
          statements
     ...
     spam(2,3)
     spam(2,3,4)
     spam(2,3,4,"Dave")
     spam(2,3,d="Dave")

General Comments

  • No non-optional arguments can follow an optional argument.
  • Can mix non-keyword and keyword arguments together when calling (with care).
  • Value of an optional argument is determined at time of function definition.
     x = 10
     def spam(a = x):
         print a
     x = 20
     spam()             # Produces '10'
<<< O'Reilly OSCON 2000, Introduction to Python, Slide 80
July 17, 2000, beazley@cs.uchicago.edu
>>>