Functions, parameters

Paul Rubin http
Thu Feb 8 13:44:01 EST 2007


Boris Ozegovic <silovana.vjeverica at com.gmail> writes:
> >>> Poll.objects.filter(question__startswith='What')
> 
> This 'question__startswith' is the problem.  What is the common idiom for
> this type od arguments, so I can Google it?  

You can refer to function args in Python by name, e.g. define a function

   def print_power(x, y):
      print x ** y

and you can pass the parameters in by position, like in most languages:

   print_power(5, 2)    # prints "25"

You can also pass them by name, saying explicitly which arg is which
(called "keyword arguments"):

   print_power(x=5, y=2)   # also prints "25"
   print_power(y=5, x=2)  # prints "32"

You can make functions that take arbitrary named parameters.  The ** below
means that arg gets bound to a dictionary containing all the keyword args:

   def func(**keyword_args):
      print 'args are:'
      for k in keyword_args:
         print k, '=>', keyword_args[k]

   func(a=2, b=5, c='whee')

prints:

    a => 2
    b => 5
    c => whee



More information about the Python-list mailing list