Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的条件表达式';s文件_Python_Python 3.x - Fatal编程技术网

Python中的条件表达式';s文件

Python中的条件表达式';s文件,python,python-3.x,Python,Python 3.x,如果我理解正确的话,在Python3中,条件表达式的形式是“x If C else y”。例如,如果我理解正确,下面的块是一个返回字符串“a”的条件表达式 'a' if 0<1 else 'b' 在Python语言中,参考由以下行给出: conditional_expression ::= or_test ["if" or_test "else" expression] 我不理解这个规则,因为举例来说,我上面的例子会说,'a'和0>1是从或_test派生出来的,'b'是从表达式

如果我理解正确的话,在Python3中,条件表达式的形式是“x If C else y”。例如,如果我理解正确,下面的块是一个返回字符串“a”的条件表达式

'a' if 0<1 else 'b'
在Python语言中,参考由以下行给出:

conditional_expression ::=  or_test ["if" or_test "else" expression]   
我不理解这个规则,因为举例来说,我上面的例子会说,
'a'
0>1
是从
或_test
派生出来的,
'b'
是从
表达式
派生出来的,而对我来说,
'a'
'b'
都应该由相同的规则派生出来(
expr
这里)。对我来说唯一有意义的事情就是用

conditional_expression ::=  expression \["if" or_test "else" expression\]

我在这里遗漏了什么?

简言之:因为这会引起歧义

我不理解这个规则,因为举例来说,我上面的例子会说,
'a'
0>1
是从
或_test
派生出来的,
'b'
是从表达式派生出来的,而对我来说,
'a'
'b'
都应该由相同的规则派生出来(
expr

嗯,
或_test
expr
都可以生成字符串文本。事实上,如果我们向下移动语法,我们将看到:

or_test  ::=  and_test | or_test "or" and_test
and_test ::=  not_test | and_test "and" not_test
not_test ::=  comparison | "not" not_test
comparison    ::=  or_expr ( comp_operator or_expr )*
or_expr  ::=  xor_expr | or_expr "|" xor_expr
xor_expr ::=  and_expr | xor_expr "^" and_expr
and_expr ::=  shift_expr | and_expr "&" shift_expr
shift_expr ::=  a_expr | shift_expr ( ">" ) a_expr
a_expr ::=  m_expr | a_expr "+" m_expr | a_expr "-" m_expr
m_expr ::=  u_expr | m_expr "*" u_expr | m_expr "//" u_expr | m_expr "/" u_expr
            | m_expr "%" u_expr
u_expr ::=  power | "-" u_expr | "+" u_expr | "~" u_expr
power ::=  primary ["**" u_expr]
primary ::=  atom | attributeref | subscription | slicing | call
atom      ::=  identifier | literal | enclosure
literal ::=  stringliteral | bytesliteral
             | integer | floatnumber | imagnumber
现在我们可以用Python编写,因为Python可以派生出
'a'
属于
a
属于第二个
,如果c else'c'
'b'属于
表达式。如果前者是表达式,我们还可以将
'a'如果a else'b'
匹配为表达式(根据Python标准,这是错误的)

使用lambda表达式也是如此。如果我们写:

lambda a, b: 'a' if a else 'b' if c else 'c'
然后Python会将其解析为:

lambda a, b: ('a' if a else 'b' if c else 'c')
但是如果我们在第一个
或_test
中启用lambda表达式,那么Python也可以将其解析为:

(lambda a, b: 'a') if a else 'b' if c else 'c'

这本身并没有错,但Python的设计者选择了前者而不是后者。重要的是,只能有一种解释,否则两个人可以用不同的解释程序运行同一个Python程序,并获得不同的结果。

这将导致如果您rite
'a'if 0>1 else'b'if 1>2 else'c'
解析起来很模糊。也许值得一看介绍它的PEP:@WillemVanOnsem:我明白了这一点,它澄清了我的疑问。谢谢。很好的解释,谢谢你展示了哪些语法规则被应用到了stringliteral,或者_test可以达到stringliteral。
lambda a, b: ('a' if a else 'b' if c else 'c')
(lambda a, b: 'a') if a else 'b' if c else 'c'