1、给 b 变量设定一个默认的值
如果实参传入的时候,指定了 b 的值,那 b 优先选择传入的实参,当 b 没有值时,才会用默认值
1
2
3
4
5
|
def funcA(a,b = 0 ):
print (a)
print (b)
funcA( 1 ) # b 变量选择默认实参
funcA( 10 , 20 ) # b 变量选择传入实参
|
返回结果:
1
0
10
20
2、参数为 tuple
1
2
3
4
5
6
|
def funT(a,b, * c):
print (a)
print (b)
print ( "length of c is : %d" % len (c))
print (c)
funT( 1 , 2 , 3 , 4 , 5 , 6 )
|
返回结果:
1
2
length of c is : 4
(3, 4, 5, 6)
3、参数为 dictionary
1
2
3
4
5
6
7
|
def funD(a, * * b):
print (a)
for x in b:
print (x + ":" + str (b[x]))
funD( 100 ,x = "hello" ,y = "你好" )
args = { "1" : "a" , "2" : "b" } # 定义一个字典
funD( 100 , * * args)
|
返回结果:
100
x:hello
y:你好
100
1:a
2:b
本文转自 归来仍少年 51CTO博客,原文链接:http://blog.51cto.com/shaoniana/1980550