This is a pretty readable chunk of code I think,
我认为这是一个非常易读的代码块,
for i in range(100):
continue if i % 2 == 0
But it's not syntactically correct. We can do other nice things in Python like,
但它在语法上并不正确。我们可以在Python中做其他好事,比如
for i in things:
total += 3 if i % 2 == 0 else 1
Or maybe,
或者可能,
return a if b > a else c
Why can't we do a continue if
statement?
为什么我们不能继续if声明?
3 个解决方案
#1
11
The flow:
流程:
for i in range(100):
continue if i % 2 == 0
Would be equivalent to:
相当于:
for i in range(1, 100, 2):
...
Or, more generically, to:
或者,更一般地说,:
for i in range(100):
if i % 2 == 0:
continue
Python language designers have a history of voting against changes to the grammar which are only offering slightly different ways of doing the same thing ("There should be one obvious way to do it").
Python语言设计者有一个投票反对语法变化的历史,它只提供了略微不同的方法来做同样的事情(“应该有一种明显的方法来做”)。
The type of one-liner construct which you've mentioned
您提到的单线构造的类型
x if cond else y
was an exception made, here. It was added to the language to offer a less error-prone way of achieving what many users were already attempting to achieve with and
and or
short-circuiting hacks (source: Guido). Code in the wild was using:
这是一个例外。它被添加到语言中,以提供一种不易出错的方式来实现许多用户已经尝试使用和/或短路黑客(来源:Guido)。野外代码使用:
cond and x or y
Which is not logically equivalent, yet it's an easy mistake to make for users who were already familiar with the ternary cond ? : x : y
syntax from C. A correct equivalent is:
这在逻辑上是不相同的,但对于已经熟悉三元cond的用户来说,这是一个容易犯的错误? :来自C的x:y语法。正确的等价物是:
(cond and [x] or [y])[0]
But, that's ugly. So, the rationale for the addition of an expression x if cond else y
was stronger than a mere convenience.
但是,这很难看。因此,如果加上其他y,则添加表达式x的基本原理强于仅仅是方便。
#2
1
Because x if cond else y
is actually an expression. Expressions are statements which evaluate to a value, in this case, either x
or y
.
因为x if cond else y实际上是一个表达式。表达式是对值进行求值的语句,在本例中为x或y。
continue
is not a value, so there's that. Also,
继续不是一个价值,所以就是这样。也,
if cond:
continue
is really not much harder or more "error prone" than continue if cond
, whereas v = x if cond else y
is probably better than
实际上并没有比cond更难或更容易出错,而v = x如果cond else y可能比
if cond:
v = x
else:
v = y
There's also the fact that if we allowed continue if cond
, we add a new way to use this _ if cond
pattern, i.e. we allow it without an else
.
还有一个事实是,如果我们允许继续cond,我们添加一种新方法来使用这个_如果cond模式,即我们允许它没有其他。
For more info: https://docs.python.org/2.5/whatsnew/pep-308.html
有关更多信息:https://docs.python.org/2.5/whatsnew/pep-308.html
#3
0
Python does have such a thing, the syntax is just a bit different. Instead of the "if and "continue" being combined as one statement, they are separated into a conditional statement (if, while etc), and a control flow (continue, pass, break etc) if it evaluates to true. In your first code example, the syntax would be:
Python确实有这样的东西,语法有点不同。而不是将“if和”继续“组合为一个语句,如果它的计算结果为true,则将它们分为条件语句(if,while等)和控制流(continue,pass,break等)。代码示例,语法为:
for i in range(100):
if i % 2 == 0:
continue
else:
#you could also add an else like this do something else if the
#number evaluated to odd
This will move on to the next iteration of the outer loop.There are also other helpful iteration tools like this, called "Control Flow Tools." I'll include a link to the Python docs that explain this. There's a ton of useful stuff there, so please do have a look.
这将继续进行外循环的下一次迭代。还有其他有用的迭代工具,称为“控制流工具”。我将包含一个解释这个的Python文档的链接。那里有很多有用的东西,所以请看看。
Others here have also suggested a single-line syntax, which works too. It is, however, good to understand both approaches; this way you can keep your code as simple as possible, but you'll also have the ability to nest loops and conditions if your algorithm will benefit from it.
这里的其他人也提出了单行语法,它也有效。然而,了解这两种方法是有益的;通过这种方式,您可以使代码尽可能简单,但如果您的算法将从中受益,您还可以嵌套循环和条件。
Happy coding!
快乐的编码!
https://docs.python.org/3/tutorial/controlflow.html
https://docs.python.org/3/tutorial/controlflow.html
#1
11
The flow:
流程:
for i in range(100):
continue if i % 2 == 0
Would be equivalent to:
相当于:
for i in range(1, 100, 2):
...
Or, more generically, to:
或者,更一般地说,:
for i in range(100):
if i % 2 == 0:
continue
Python language designers have a history of voting against changes to the grammar which are only offering slightly different ways of doing the same thing ("There should be one obvious way to do it").
Python语言设计者有一个投票反对语法变化的历史,它只提供了略微不同的方法来做同样的事情(“应该有一种明显的方法来做”)。
The type of one-liner construct which you've mentioned
您提到的单线构造的类型
x if cond else y
was an exception made, here. It was added to the language to offer a less error-prone way of achieving what many users were already attempting to achieve with and
and or
short-circuiting hacks (source: Guido). Code in the wild was using:
这是一个例外。它被添加到语言中,以提供一种不易出错的方式来实现许多用户已经尝试使用和/或短路黑客(来源:Guido)。野外代码使用:
cond and x or y
Which is not logically equivalent, yet it's an easy mistake to make for users who were already familiar with the ternary cond ? : x : y
syntax from C. A correct equivalent is:
这在逻辑上是不相同的,但对于已经熟悉三元cond的用户来说,这是一个容易犯的错误? :来自C的x:y语法。正确的等价物是:
(cond and [x] or [y])[0]
But, that's ugly. So, the rationale for the addition of an expression x if cond else y
was stronger than a mere convenience.
但是,这很难看。因此,如果加上其他y,则添加表达式x的基本原理强于仅仅是方便。
#2
1
Because x if cond else y
is actually an expression. Expressions are statements which evaluate to a value, in this case, either x
or y
.
因为x if cond else y实际上是一个表达式。表达式是对值进行求值的语句,在本例中为x或y。
continue
is not a value, so there's that. Also,
继续不是一个价值,所以就是这样。也,
if cond:
continue
is really not much harder or more "error prone" than continue if cond
, whereas v = x if cond else y
is probably better than
实际上并没有比cond更难或更容易出错,而v = x如果cond else y可能比
if cond:
v = x
else:
v = y
There's also the fact that if we allowed continue if cond
, we add a new way to use this _ if cond
pattern, i.e. we allow it without an else
.
还有一个事实是,如果我们允许继续cond,我们添加一种新方法来使用这个_如果cond模式,即我们允许它没有其他。
For more info: https://docs.python.org/2.5/whatsnew/pep-308.html
有关更多信息:https://docs.python.org/2.5/whatsnew/pep-308.html
#3
0
Python does have such a thing, the syntax is just a bit different. Instead of the "if and "continue" being combined as one statement, they are separated into a conditional statement (if, while etc), and a control flow (continue, pass, break etc) if it evaluates to true. In your first code example, the syntax would be:
Python确实有这样的东西,语法有点不同。而不是将“if和”继续“组合为一个语句,如果它的计算结果为true,则将它们分为条件语句(if,while等)和控制流(continue,pass,break等)。代码示例,语法为:
for i in range(100):
if i % 2 == 0:
continue
else:
#you could also add an else like this do something else if the
#number evaluated to odd
This will move on to the next iteration of the outer loop.There are also other helpful iteration tools like this, called "Control Flow Tools." I'll include a link to the Python docs that explain this. There's a ton of useful stuff there, so please do have a look.
这将继续进行外循环的下一次迭代。还有其他有用的迭代工具,称为“控制流工具”。我将包含一个解释这个的Python文档的链接。那里有很多有用的东西,所以请看看。
Others here have also suggested a single-line syntax, which works too. It is, however, good to understand both approaches; this way you can keep your code as simple as possible, but you'll also have the ability to nest loops and conditions if your algorithm will benefit from it.
这里的其他人也提出了单行语法,它也有效。然而,了解这两种方法是有益的;通过这种方式,您可以使代码尽可能简单,但如果您的算法将从中受益,您还可以嵌套循环和条件。
Happy coding!
快乐的编码!
https://docs.python.org/3/tutorial/controlflow.html
https://docs.python.org/3/tutorial/controlflow.html