2012年11月6日 星期二
Python - about *, **, *args, **kwargs
* : use to pass list/tuple as positional argument to a function
ex. def func(a, b, c)
print a, b, c
l = [1,2,3]
func(*l)
1,2,3
** : use to pass dictionary as named argument to a function
ex. def func(a, b, c)
print a, b, c
d ={a:1, b:2, c:3}
func(**l)
1,2,3
*args : use to define a function to receive a arbitary arguments as list
ex. def func(a, *args)
print a, args
func(1, 2, 3, 4, 5)
1, (2, 3, 4, 5)
*args : use to define a function to receive a arbitary arguments as dictionary
ex. def func(a, **kwargs)
print a, args
func(1, b=2, c=3, d=4, e=5)
1, ('b':2, 'c':3,'d':4, 'e':5)
This is just my simple note, more in details refer to this very good post : http://agiliq.com/blog/2012/06/understanding-args-and-kwargs/
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言