这是我的行动:
>>> def show(d):
print d
...
>>> test = {"result": True}
>>> show(test)
{'result': True}
>>> show(test["info"]="Some info")
File "<console>", line 1
SyntaxError: keyword can't be an expression
Run Code Online (Sandbox Code Playgroud)
为什么我不能将表达式作为参数传递给函数?
不允许在Python表达式中赋值的原因是其他语言中的一个常见的,难以发现的错误,由以下结构引起:
if (x = 0) {
// error handling
}
else {
// code that only works for nonzero x
}
Run Code Online (Sandbox Code Playgroud)
来源:http : //docs.python.org/faq/design.html#why-can-ti-use-an-assignment-in-an-expression
| 归档时间: |
|
| 查看次数: |
20793 次 |
| 最近记录: |