在python三元条件的任一侧使用语句 [英] Using statements on either side of a python ternary conditional

查看:35
本文介绍了在python三元条件的任一侧使用语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么禁止在python的三元条件两边使用语句?我看不出任何明显的理由说明以下一些幼稚的语法示例可能含糊不清或被破坏 - 但我相信一定有一个很好的理由为什么它被禁止!

<预><代码>>>>x, y = 0, 0>>>(x += 1) if random.choice([0, 1]) else (y += 1)^语法错误:无效语法>>>(x if random.choice([0, 1]) else y) += 1语法错误:无法分配给条件表达式>>>打印 'hello world' if random.choice([0, 1]) else raise StandardError()文件<stdin>",第 1 行打印 'hello world' if random.choice([0, 1]) else raise StandardError()^语法错误:无效语法

您能否举一个例子,说明允许在三元组中使用语句可能是危险的或定义不明确的?

解决方案

表达式被eval使用,语句被exec使用.这是完全不同的东西.

Python 开发人员决定严格区分表达式和语句.这将使您过早失败",而不是以难以调试的错误告终.

在其他语言中,比如 C(++),你可以做类似的事情

if (i=15) {...}

但你也可以这样做

if (i==15) {...}

在第一个示例中,您将 15 分配给变量 i,然后通过 if 评估 i 的这个新值.

更糟糕的是递增/递减运算符.

和有什么不一样

if (i--) {...}

if (--i) {...}

?

对于第一个示例,评估 i 的值,然后递增.对于第二个例子,顺序是相反的,所以如果 i 之前是 1,结果是不同的.

在 Python 中,由于在其他语言中收集了此类概念的经验,因此语法禁止此类内容.

我不太确定这是否完全回答了您的问题,但我希望我至少可以对 Pythonic 方法提供一些见解.

Why is it prohibited to use statements on either side of python's ternary conditional? I can't see any obvious reason how a few of the following naive syntax examples could be ambiguous or broken - but I'm sure there must be a good reason why it's disallowed!

>>> x, y = 0, 0
>>> (x += 1) if random.choice([0, 1]) else (y += 1)
        ^
SyntaxError: invalid syntax

>>> (x if random.choice([0, 1]) else y) += 1
SyntaxError: can't assign to conditional expression


>>> print 'hello world' if random.choice([0, 1]) else raise StandardError()
  File "<stdin>", line 1
    print 'hello world' if random.choice([0, 1]) else raise StandardError()
                                                          ^
SyntaxError: invalid syntax

Can you give an example where allowing a statement to be used in a ternary could be dangerous or ill-defined?

解决方案

Expressions are evaluated, statements are executed. This is something completely different.

Python developers have decided to strictly separate between expressions and statements . This will make you "fail early" instead of ending up with an error that is hard to debug.

In other languages, like C(++), you can do things like

if (i=15) {...}

but you could also do

if (i==15) {...}

In the first example, you assign 15 to the variable i, and this new value of i is then evaluated by the if.

Even worse are increment/decrement operators. What is the difference between

if (i--) {...}

and

if (--i) {...}

?

For the first example, the value of i is evaluated, then it is incremented. For the second example, the order is the other way around, so if i was 1 before, the result is different.

In Python, such things are prohibited by the syntax, due to the experience gathered with such concepts in other languages.

I'm not quite sure if this completely answers your question, but I hope I at least could give some insight into the pythonic approach.

这篇关于在python三元条件的任一侧使用语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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