例子:
def f(x):
print x
f(10) #输出 10
f('hello') #输出hello
f([1,2,3,4]) #输出[1,2,3,4]
f(range(10)) # 输出[0,1,2,3,4,5,6,7,8,9]
f(('a','b')) #输出('a','b')
f({1:111,2:222,3:333}) #输出{1:111,2:222,3:333}
f('a','b') #出错
例子2:
def f(x,y):
print x,y
f(1,2) #输出 1 2
f('a','b') #输出 a b
t = ('c','d')
f(t) #出错
f(t,test) #输出 ('c','d') test
例子3:
def f(x,y):
print "%s : %s" %(x,y)
t = ('name','milo')
f(t) #出错
f(*t) #输出 name:milo
tt = ('name','milo',30)
f(*tt) #出错
例子4:
def f(x,*args):
print x
print args
f(1) #输出1
()
f(1,2,3) #输出 1
(2,3)
f(x=3) #输出 4
()
f(x=1,y=2) #出错
例子5:
def f(x,*args,**kwargs):
print x
print args
print kwargs
f(1) #输出 1
()
{}
f(1,2,3) #输出 1
(2,3)
{}
f(x=1,y=2) #输出 1
()
{'y':2}
f(1,2,3,y=11,z=12) #输出 1
(2,3)
{'y':11,'z':12}