可能重复:
*args和**kwargs是什么意思?
从阅读这个例子和我对Python的渺小知识来看,它必须是将数组转换为字典的快捷方式吗?
class hello:
def GET(self, name):
return render.hello(name=name)
# Another way:
#return render.hello(**locals())
Run Code Online (Sandbox Code Playgroud)
sth*_*sth 11
在python中f(**d),将字典中的值d作为关键字参数传递给函数f.类似地f(*a),将数组中的值a作为位置参数传递.
举个例子:
def f(count, msg):
for i in range(count):
print msg
Run Code Online (Sandbox Code Playgroud)
使用**d或调用此函数*a:
>>> d = {'count': 2, 'msg': "abc"}
>>> f(**d)
abc
abc
>>> a = [1, "xyz"]
>>> f(*a)
xyz
Run Code Online (Sandbox Code Playgroud)
它将字典“解包”为参数列表。IE:
def somefunction(keyword1, anotherkeyword):
pass
Run Code Online (Sandbox Code Playgroud)
它可以被称为
somefunction(keyword1=something, anotherkeyword=something)
or as
di = {'keyword1' : 'something', anotherkeyword : 'something'}
somefunction(**di)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2471 次 |
| 最近记录: |