PEP-315(“do”循环) [英] PEP-315 ("do" loop)

查看:105
本文介绍了PEP-315(“do”循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于PEP-315的两个观察结果:


1.它很聪明,解决了明确的疣,并且在语法上类似于
类似尝试/除外。但它的语法似乎是一种后天的味道。


2.这是一个非常通用的结构,可能是所谓的。

但是我想知道大部分时间它是否会用来完成

之类的东西:


而1:

line = sys.stdin.readline()

如果行==" \ n":

break


以更整洁的方式?相反,如何创建一个直到循环:


直到行==" \ n":

line = sys.stdin.readline()


将被定义为完全像while循环一样工作,除了测试是第一次通过循环时没有评估
。这不是类似于在循环结束时检查

,同时保持更多的while-ish

语法吗?


这是否有用或者是按照PEP-315的顺序

去?

解决方案

< blockquote> [Wayne Folta]

关于PEP-315的两个观察结果:
.. 。

以更整洁的方式?相反,如何创建一个直到循环:

直到行==" \\\
":
line = sys.stdin.readline()

将被定义为完全像while循环除了测试之外没有第一次通过循环评估。这不是类似于在循环结束时进行检查,同时保持更多的while语法吗?




虽然它有效,或者我应该说直到它不起作用,有两个问题。首先,这不是直到的通常含义。在其他

语言中,语义就像while条件

倒置。其次,更清楚的是将条件置于程序中的评估中(在循环结束时,

不是开头)。

Raymond Hettinger




" Wayne Folta" < WF **** @ netmail.to>在留言中写道

新闻:ma ************************************ * @ pytho n.org ...

关于PEP-315的两个观察结果:


1.它很聪明,可以解决明确的疣问题;,并且在语法上类似于

try / except。但它的语法对我来说似乎是一种后天的味道。


2.这是一个非常通用的结构,可能就是所要求的。但是我想知道大部分时间它是否会被用来完成类似的事情:


而1:

line = sys.stdin.readline()

if line ==" \ n":

break


在一个整洁的办法?相反,如何创建一个直到循环:


直到行==" \ n":

line = sys.stdin.readline()


将被定义为完全像while循环一样工作,除了测试不是第一次通过循环评估
。这不是类似于在循环结束时检查

,同时保持更多的语法吗?


这是否有用或者是PEP-315的顺序是什么?


[回复]


1.我已经重新缩进你的例子 - 他们没有在某些

浏览器下正确缩进。


2.请使用纯文本 - 你的消息没有正确引用的原因你使用了其他东西,而且我的新闻客户端打破了格式化。


现在回复实质性的回复。


1.建议的语法以某种方式打破了Python的缩进方案,其中包括:
try-except,for-else,while-else和if-elif-else不这样做。 />

2.我发现更严重的是有很多方法可以进入

改善基本的while循环,其中没有一个似乎是一个自然的
停止点。 Python的理念似乎是找到甜蜜点

,你用最少量的机制获得最大的杠杆。

既不是PEP-315也不是任何改进我见过这样做。


John Roth


Wayne Folta< wf **** @ netmail.to> ;在消息新闻中写道:< ma ************************************* @ pyth on.org> ; ...

2.这是一个非常通用的构造,可能就是所要求的。
但我想知道大部分时间它是否会被用来完成类似于:

而1:
line = sys.stdin.readline()
如果行==" \ n":
中断




如果你担心美女这样的代码,然后

重写它的适当方法是使用自定义迭代器/生成器。


def untilBlankLine(fh):

line = fh.readline()

while line!=''\ n'':

产量线

line = fh.readline()


然后你可以这样使用它:


for line in untilBlankLine(sys.stdin):

doSomething(行)


请记住,您还可以使用内置函数创建自定义迭代器

iter:


for iter(sys.stdin.next,''\ n''):

doSomething(line)


所以我真的认为不需要任何更复杂的循环结构。

我们进一步鼓励使用while而不是

更好。


Jeremy


Two observations about PEP-315:

1. It''s clever, addresses a definite "wart", and is syntactically
similar to try/except. But it''s syntax seems like an acquired taste to
me.

2. It is a very general construct, which might be what is called for.
But I wonder if most of the time it would be used to accomplish
something like:

while 1:
line = sys.stdin.readline()
if line == "\n":
break

In a neater way? How about, instead, creating an "until" loop:

until line == "\n":
line = sys.stdin.readline()

Would be defined to work exactly like a while loop except the test is
not evaluated the first time through the loop. Wouldn''t this be akin to
checking at the end of the loop, while maintaining a more while-ish
syntax?

Is this at all useful or is something of the order of PEP-315 the way
to go?

解决方案

[Wayne Folta]

Two observations about PEP-315: .. . .
In a neater way? How about, instead, creating an "until" loop:

until line == "\n":
line = sys.stdin.readline()

Would be defined to work exactly like a while loop except the test is
not evaluated the first time through the loop. Wouldn''t this be akin to
checking at the end of the loop, while maintaining a more while-ish
syntax?



While it works, or should I say "until it doesn''t work", there are two
issues. First, this is not the usual meaning of "until" in other
languages where the semantics is like "while" with the condition
inverted. Second, it is much more clear to put the condition at the
point in the program that it gets evaluated (at the end of the loop,
not the beginning).
Raymond Hettinger



"Wayne Folta" <wf****@netmail.to> wrote in message
news:ma*************************************@pytho n.org...
Two observations about PEP-315:

1. It''s clever, addresses a definite "wart", and is syntactically similar to
try/except. But it''s syntax seems like an acquired taste to me.

2. It is a very general construct, which might be what is called for. But I
wonder if most of the time it would be used to accomplish something like:

while 1:
line = sys.stdin.readline()
if line == "\n":
break

In a neater way? How about, instead, creating an "until" loop:

until line == "\n":
line = sys.stdin.readline()

Would be defined to work exactly like a while loop except the test is not
evaluated the first time through the loop. Wouldn''t this be akin to checking
at the end of the loop, while maintaining a more while-ish syntax?

Is this at all useful or is something of the order of PEP-315 the way to go?

[response]

1. I''ve reindented your examples - they don''t indent properly under some
browser.

2. Please use plain text - the reason your message isn''t quoted properly is
that you used something else, and my news client breaks the formatting.

Now to the substantive responses.

1. The proposed syntax breaks Python''s indentation scheme in a manner that
try-except, for-else, while-else, and if-elif-else do not.

2. What I find more serious is that there are a huge number of ways to go in
"improving" the basic while loop, none of which seem to be a natural
stopping point. Python''s philosophy seems to be to find the "sweet spot"
where you gain the most leverage with the minimum amount of mechanism.
Neither PEP-315 nor any of the "improvements" I''ve seen do that.

John Roth


Wayne Folta <wf****@netmail.to> wrote in message news:<ma*************************************@pyth on.org>...

2. It is a very general construct, which might be what is called for.
But I wonder if most of the time it would be used to accomplish
something like:

while 1:
line = sys.stdin.readline()
if line == "\n":
break



If you''re concerned about the "beauty" of such code, then the
appropriate way to rewrite it is with a custom iterator/generator.

def untilBlankLine(fh):
line = fh.readline()
while line != ''\n'':
yield line
line = fh.readline()

And then you can use it like so:

for line in untilBlankLine(sys.stdin):
doSomething(line)

And remember, you can also create custom iterators with the builtin
iter:

for line in iter(sys.stdin.next, ''\n''):
doSomething(line)

So I really see no need for any more complex looping construct. The
further we get from encouraging the use of while instead of for, the
better.

Jeremy


这篇关于PEP-315(“do”循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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