Friday, March 28, 2008

python: non-defined function arguments

You know that you can get named arguments of the routine that are not in function definition with **kw parameter:

def func(**kw):
     for k,v in kw.items(): print "%s: %s" % (k,v)
kw is a dict here where key is parameter name and value is parameter value. So if you call
func(a=1, b=2)
your kw will be
{'a': 1, 'b': 2}
>>> func(a=1, b=2)
a: 1
b: 2
The same is for the unnamed arguments:
def func(*a):
     for v in a: print v
a is a list of arguments here
func(1, 2)
will produce tuple a
(1, 2)
>>> func(1, 2)
(1, 2)
Nice I should say. Let's view the other side. You have function
def func(a=1, b=2):
     print a, b
and you have a dict
kw = {'a': 3, 'b': 4}
Now you just can use some py magic
func(**kw)
You will get a=3 and b=4:
func(**kw)
3 4
And for the unnamed arguments
def func(a, b):
     print a, b
If you define a list of 2 values
a = (1, 2)
and pass it to the func
func(*a)
you will get a=1 and b=2
func(*a)
1 2
Some extra py magic for these tricks:
def func(a=1, b=2): pass
func(**{'a': 3, 'b': 4})
def func(a, b): pass
func(*(1, 2))

3 comments:

Cheba said...

WTF? Pointer dereferencing?..

Ni@m said...

I assume that's a part of py syntax.
From manual:
If the form ``*identifier'' is present, it is initialized to a tuple receiving any excess positional parameters, defaulting to the empty tuple. If the form ``**identifier'' is present, it is initialized to a new dictionary receiving any excess keyword arguments, defaulting to a new empty dictionary.
Asterisk is just a syntax construction for function definition and arguments passing.

Cheba said...

As for me Rubys trailing hash syntax is much more flexible and elegant.