Main Content

Function / method parameters with * and ** in Python

Archive - Originally posted on "The Horse's Mouth" - 2007-04-04 00:39:41 - Graham Ellis

In Python, you can define a function with optional parameters; if you specify a single * in front of a parameter, then that will be a tuple of all remaining unnamed values. And if you specify two *s in front of a parameter, that will be a dictionary of all remaining named parameters.

Let's see an example:

def demo(first, second, *third, **fourth):
  print "first",first
  print "second",second
  print "third",third
  print "fourth",fourth
  print
 
demo(1,2,3,4,5,6,7)
 
demo(5,6,7,8,aa=9,bb=11,cc=44)


When I run that, I get

C:\gje>python onetwo.py
first 1
second 2
third (3, 4, 5, 6, 7)
fourth {}
 
first 5
second 6
third (7, 8)
fourth {'aa': 9, 'cc': 44, 'bb': 11}
 
C:\gje>


Note that * and ** parameters must be the last parameters in a function definition, and if you're going to use both of them you put the ** at the very end, after the *.