1. def foo(p1,p2,p3,...):
列出有限个数的参数,并且彼此之间用逗号隔开。在调用函数的时候,按照顺序以此对参数进行赋值,特备注意的是,参数的名字不重要,重要的是位置。而且,必须数量一致,一一对应。第一个对象(可能是数值、字符串等等)对应第一个参数,第二个对应第二个参数,如此对应,不得偏左也不得偏右。
2. def foo(p1=value1,p2=value2,...)
3.def foo(*args)
4.def foo(**args)
例:
>>> def foo(x,y=2,*targs,**dargs):
... print "x==>",x
... print "y==>",y
... print "targs_tuple==>",targs
... print "dargs_dict==>",dargs
...
>>> foo("1x")
x==> 1x
y==> 2
targs_tuple==> ()
dargs_dict==> {}
>>> foo("1x","2y")
x==> 1x
y==> 2y
targs_tuple==> ()
dargs_dict==> {}
>>> foo("1x","2y","3t1","3t2")
x==> 1x
y==> 2y
targs_tuple==> ('3t1', '3t2')
dargs_dict==> {}
>>> foo("1x","2y","3t1","3t2",d1="4d1",d2="4d2")
x==> 1x
y==> 2y
targs_tuple==> ('3t1', '3t2')
dargs_dict==> {'d2': '4d2', 'd1': '4d1'}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30319453/viewspace-2121771/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/30319453/viewspace-2121771/