while 循环内的代码冗余很小(感觉不干净)
所以,在Python中(虽然我认为它可以应用于许多语言),我发现自己经常遇到这样的事情:
the_input = raw_input("what to print?\n")
while the_input != "quit":
print the_input
the_input = raw_input("what to print?\n")
也许我太挑剔了,但我不喜欢行 the_input = raw_input (“要打印什么?\n”)
必须重复。它降低了可维护性和组织性。但我没有看到任何解决方法可以避免重复代码而不进一步恶化问题。在某些语言中,我可以写这样的东西:
while ((the_input=raw_input("what to print?\n")) != "quit") {
print the_input
}
这绝对不是 Pythonic,而且 Python 甚至不允许在循环条件内进行赋值 AFAIK。
这个有效的代码修复了冗余,
while 1:
the_input = raw_input("what to print?\n")
if the_input == "quit":
break
print the_input
但感觉也不太正确。 while 1
意味着该循环将永远运行;我正在使用一个循环,但给它一个假条件并将真实条件放入其中。
是我太挑剔了吗?有更好的方法吗?也许有一些我不知道的为此设计的语言结构?
So, in Python (though I think it can be applied to many languages), I find myself with something like this quite often:
the_input = raw_input("what to print?\n")
while the_input != "quit":
print the_input
the_input = raw_input("what to print?\n")
Maybe I'm being too picky, but I don't like how the line the_input = raw_input("what to print?\n")
has to get repeated. It decreases maintainability and organization. But I don't see any workarounds for avoiding the duplicate code without further worsening the problem. In some languages, I could write something like this:
while ((the_input=raw_input("what to print?\n")) != "quit") {
print the_input
}
This is definitely not Pythonic, and Python doesn't even allow for assignment within loop conditions AFAIK.
This valid code fixes the redundancy,
while 1:
the_input = raw_input("what to print?\n")
if the_input == "quit":
break
print the_input
But doesn't feel quite right either. The while 1
implies that this loop will run forever; I'm using a loop, but giving it a fake condition and putting the real one inside it.
Am I being too picky? Is there a better way to do this? Perhaps there's some language construct designed for this that I don't know of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想想迭代器——例如,在这种特定情况下:
Python 中的大多数循环(除了最低抽象级别的循环)最好在一些函数的帮助下实现为
for
循环。捕获“循环逻辑”的底层迭代器 - 内置的iter
可以提供帮助(就像这里),有时genexps(生成器表达式)可以,有时标准库模块itertools
来救援。大多数情况下,您会选择编写自定义生成器函数(使用
yield
),或更偶尔(当您需要真正复杂的状态管理时)自定义迭代器类< /em> (定义 __iter__ 特殊方法为return self
,以及next
[[or__next__
最新版本的 Python]] 返回“迭代中的下一个值”。捕获循环逻辑,而不是对循环本身按顺序生成的各种项目执行的任何操作,这是关键的抽象-这里有帮手!
Think iterators -- e.g., in this specific case:
Most loops in Python, except at the very lowest levels of abstractions, are best implemented as
for
loops with the help of some underling iterator which captures the "looping logic" -- theiter
built-in can help (like here), sometimes genexps (generator expressions) can, sometimes the standard library moduleitertools
comes to the rescue.Most often you will choose to code custom generator functions (ones using
yield
), or more occasionally (when you need really sophisticated state management) a custom iterator class (one defining the__iter__
special method asreturn self
, andnext
[[or__next__
in the latest versions of Python]] to return "the next value from the iteration).Capturing the looping logic apart from whatever it is that you do on the various items sequentially produced by the loop itself is the key abstraction-helper here!