一、定义
def functionName([arg1,arg2,...]): code
二、示例
#!/usr/bin/python #coding:utf8 #coding=utf8 #encoding:utf8 #encoding=utf8 def fun(x=3,y=‘甜‘): print ‘生成一个‘,x,‘元‘,y,‘口味的蛋糕‘ fun() fun(10) fun(y=‘奶油‘)
冗余参数:
#!/usr/bin/python def f(name="jim",age=20): print "name:%s age:%s" % (name,age) t=(‘tom‘, 25) d={‘age‘:25,‘name‘:‘lucy‘} f(*t) #name:tom age:25 f(**d) #name:lucy age:25 def f1(x,*args): print x,args t1=(‘tom‘,25,‘male‘) f1(*t1) #tom (25,‘male‘) def f2(x,**args): print x,args d1={‘x‘:1,‘y‘:2} f2(**d1) #1 {‘y‘:2} def f3(x,*arg1,**arg2): print x print arg1 print arg2 f3(1,2,3,a=1,b=2,c=3) #1 #(2,3) #{‘a‘:1,‘b‘:2,‘c‘:3}
时间: 2024-11-20 01:28:59