将表达式作为参数传递:关键字不能是表达式 [英] Passing expression as argument: keyword can't be an expression

查看:59
本文介绍了将表达式作为参数传递:关键字不能是表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的行为:

<预><代码>>>>定义显示(d):打印...>>>测试 = {结果":真}>>>显示(测试){'结果':真}>>>显示(测试[信息"] =一些信息")文件<控制台>",第 1 行语法错误:关键字不能是表达式

为什么我不能将表达式作为参数传递给函数?

解决方案

= 符号向 Python 表明这是一个关键字参数,而不是位置参数.由于 = 左边的部分是一个表达式 test["info"] 你会得到错误.

Here is my actions:

>>> 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

Why can I not pass expression as argument to a function?

解决方案

The = sign indicates to Python that this is a keyword parameter, not a positional one. Since the part to the left of the = is an expression test["info"] you get the error.

这篇关于将表达式作为参数传递:关键字不能是表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆