1. dict.update
dic = dict()
dic.update({1:2})
print dic # {1:2}
update()的参数是另一个字典,如果原字典中没有key-value对,则增加,如果有,则更新key-value。注意,dict是一个类,可以调用其构造方法
2. *args, **kwargs
用几个例子说明:
def foo(*args):
print args
print type(args)
def bar(**kwargs):
print kwargs
print type(kwargs)
def foobar(*args, **kwargs):
print args
print kwargs
测试代码:
foo(1)
(1,)
<type 'tuple'>
foo(1,2)
(1, 2)
<type 'tuple'>
bar(m=1)
{'m': 1}
<type 'dict'>
bar(m=1, n=2)
{'m': 1, 'n': 2}
<type 'dict'>
foobar(1)
(1,)
{}
foobar(1,2, m=1)
(1, 2)
{'m': 1}
foobar(1,2, m=1, n=2)
(1, 2)
{'m': 1, 'n': 2}
注意,*args 必须在 **kwargs 之前
浙公网安备 33010602011771号