参数列表 — 1位置参数,2默认参数,3可变参数,4关键字参数,可变关键字参数;前后顺序必须这样,不能有丝毫交际。
def getValue(position1, default1 = "默认", *args, keyName1, **kwargs):
print("postion1:\t" + position1)
print("default1:\t" + default1)
print("args:\t\t" + str(args))
print("keyName1:\t" + keyName1)
print("kwargs\t\t" + str(kwargs))
print()
#1
getValue("matchPostion1", "matchDefault1", "matchArgs1", "matchArgs2",
keyName1 = "matchKeyName1", kwargs1 = "matchKwargs1", kwargs2 = "matchKwargs2")
#2
getValue("matchPostion1", "matchDefault1", "matchArgs1", "matchArgs2",
kwargs1 = "matchKwargs1", keyName1 = "matchKeyName1", kwargs2 = "matchKwargs2")
#3
getValue("matchPostion1", "matchDefault1", "matchArgs1", "matchArgs2",
kwargs1 = "matchKwargs1", kwargs2 = "matchKwargs2", keyName1 = "matchKeyName1")
getValue("matchPostion1", "matchDefault1", keyName1 = "matchKeyName1", "matchArgs1",
"matchArgs2", kwargs1 = "matchKwargs1", kwargs2 = "matchKwargs2")
postion1: matchPostion1
default1: matchDefault1
args: ('matchArgs1', 'matchArgs2')
keyName1: matchKeyName1
kwargs {'kwargs1': 'matchKwargs1', 'kwargs2': 'matchKwargs2'}
关键字参数和可变关键字一样无法提前,只能相互之间、在内部交换
#1
getValue("matchPostion1", keyName1 = "matchKeyName1")
#2
getValue(position1 = "matchPostion1", keyName1 = "matchKeyName1")
#3
getValue(keyName1 = "matchKeyName1", position1 = "matchPostion1")
postion1: matchPostion1
default1: 默认
args: ()
keyName1: matchKeyName1
kwargs {}
位置参数,也可以通过形参变量名对应赋值,在这种情况下不受位置限制,与关键字参数任意排列。
#1
getValue("matchPostion1", keyName1 = "matchKeyName1", kwargs1 = "matchKwargs1")
getValue("matchPostion1", kwargs1 = "matchKwargs1", keyName1 = "matchKeyName1")
#2
getValue(position1 = "matchPostion1", keyName1 = "matchKeyName1", kwargs1 = "matchKwargs1")
getValue(position1 = "matchPostion1", kwargs1 = "matchKwargs1", keyName1 = "matchKeyName1")
#3
getValue(kwargs1 = "matchKwargs1", position1 = "matchPostion1", keyName1 = "matchKeyName1")
getValue(keyName1 = "matchKeyName1", position1 = "matchPostion1", kwargs1 = "matchKwargs1")
#4
getValue(kwargs1 = "matchKwargs1", keyName1 = "matchKeyName1", position1 = "matchPostion1")
getValue(keyName1 = "matchKeyName1", kwargs1 = "matchKwargs1", position1 = "matchPostion1")
postion1: matchPostion1
default1: 默认
args: ()
keyName1: matchKeyName1
kwargs {}
位置参数,也可以通过形参变量名对应赋值,在这种情况下不受位置限制,与关键字参数、可变关键字参数任意排列。
def getValue(position1, default1 = "默认", *args, keyName1 = "默认关键字参数", **kwargs):
print("postion1:\t" + position1)
print("default1:\t" + default1)
print("args:\t\t" + str(args))
print("keyName1:\t" + keyName1)
print("kwargs\t\t" + str(kwargs))
print()
#1
getValue("matchPostion1", "matchDefault1", "matchArgs1", "matchArgs2",
keyName1 = "matchKeyName1", kwargs1 = "matchKwargs1", kwargs2 = "matchKwargs2")
#2
getValue("matchPostion1", "matchDefault1", "matchArgs1", "matchArgs2",
kwargs1 = "matchKwargs1", keyName1 = "matchKeyName1", kwargs2 = "matchKwargs2")
#3
getValue("matchPostion1", "matchDefault1", "matchArgs1", "matchArgs2",
kwargs1 = "matchKwargs1", kwargs2 = "matchKwargs2", keyName1 = "matchKeyName1")
postion1: matchPostion1
default1: matchDefault1
args: ('matchArgs1', 'matchArgs2')
keyName1: matchKeyName1
kwargs {'kwargs1': 'matchKwargs1', 'kwargs2': 'matchKwargs2'}
这种情况与关键字参数不含默认值相同,因为默认值被覆盖。
同时关键字参数和可变关键字一样§无法提前,只能相互之间、在内部交换
#1
getValue("matchPostion1")
#2
getValue(position1 = "matchPostion1")
postion1: matchPostion1
default1: 默认
args: ()
keyName1: 默认关键字参数
kwargs {}
在有可变参数*args的情况下,关键字参数有默认值,可以很好的隐藏该默认值。
位置参数,也可以通过形参变量名对应赋值。
#1
getValue("matchPostion1", kwargs1 = "matchKwargs1", kwargs2 = "matchKwargs2")
#2
getValue(position1 = "matchPostion1", kwargs1 = "matchKwargs1", kwargs2 = "matchKwargs2")
#3
getValue(kwargs1 = "matchKwargs1", position1 = "matchPostion1", kwargs2 = "matchKwargs2")
#4
getValue(kwargs1 = "matchKwargs1", kwargs2 = "matchKwargs2", position1 = "matchPostion1")
getValue("matchPostion1", "matchDefault1", "matchArgs1", "matchArgs2",
kwargs1 = "matchKwargs1", kwargs2 = "matchKwargs2")
结果
postion1: matchPostion1
default1: matchDefault1
args: ('matchArgs1', 'matchArgs2')
keyName1: 默认关键字参数
kwargs {'kwargs1': 'matchKwargs1', 'kwargs2': 'matchKwargs2'}
默认值无法隐藏,灵活性差
postion1: matchPostion1
default1: 默认
args: ()
keyName1: 默认关键字参数
kwargs {}
在有可变参数*args的情况下,关键字参数有默认值,可以很好的隐藏该默认值。
位置参数,也可以通过形参变量名对应赋值。
参见
https://blog.csdn.net/weixin_43473435/article/details/83619287
位置参数必须在默认参数前面。
调用最好老老实实与位置对应,不用<变量名=值>(避免不必要的麻烦),不然可能造成可变参数无法赋值。
参见 https://blog.csdn.net/weixin_43473435/article/details/83419290
可变参数(*args)后
不带默认值
参见 https://blog.csdn.net/weixin_43473435/article/details/83419185