无关键字参数是元组,关键字参数是字典
以下代码来自 《编程思想》
1.
*
args
def
tupleArgs(arg1, arg2
=
'B'
,
*
arg3):
print
(
'arg 1:%s '
%
arg1)
print
(
'arg 2:%s '
%
arg2)
for
eachArgNum
in
range
(
len
(arg3)):
print
(
'the %d in arg 3 :%s '
%
(eachArgNum,arg3[eachArgNum]))
if
__name__
=
=
'__main__'
:
tupleArgs(
'A'
)
# arg 1:A
# arg 2:B
tupleArgs(
'23'
,
'C'
)
# arg 1:23
# arg 2:C
tupleArgs(
'12'
,
'A'
,
'GF'
,
'L'
)
# arg 1:12
# arg 2:A
//备注:此处的arg3 = ('GF'
,
'L'
) 元组 ,顺序输出
# the 0 in arg 3 :GF
# the 1 in arg 3 :L
*
*
kw
def
dictArgs(kw1, kw2
=
'B'
,
*
*
kw3):
print
(
'kw 1:%s '
%
kw1)
print
(
'kw 2:%s '
%
kw2)
for
eachKw
in
kw3:
print
(
'the %s ---->:%s '
%
(eachKw,kw3[eachKw]))
if
__name__
=
=
'__main__'
:
dictArgs(
'A'
)
# kw 1:A
# kw 2:B
dictArgs(
'23'
,
'C'
)
# kw 1:23
# kw 2:C
dictArgs(
'12'
,
'A'
, c
=
'C'
,d
=
'12121212'
)
# kw 1:12
# kw 2:A
//备注:区别于无关键字参数,此处的kw3 = {c
=
'C'
,d
=
'12121212'} 字典 ,至于输出,是因为 字典内的元素是无序的
# the d ---->:12121212
# the c ---->:C
dictArgs(
'kw'
,c
=
'C'
,d
=
'12121212'
,kw
=
'KW'
)
# kw 1:kw
# kw 2:B
# the kw ---->:KW
# the d ---->:12121212
# the c ---->:C