while 循环内的代码冗余很小(感觉不干净)

发布于 2024-09-03 12:18:27 字数 806 浏览 2 评论 0原文

所以,在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若相惜即相离 2024-09-10 12:18:27

想想迭代器——例如,在这种特定情况下:

for the_input in iter(lambda: raw_input('what to print?\n'), 'quit'):
    print the_input

Python 中的大多数循环(除了最低抽象级别的循环)最好在一些函数的帮助下实现为 for 循环。捕获“循环逻辑”的底层迭代器 - 内置的 iter 可以提供帮助(就像这里),有时genexps(生成器表达式)可以,有时标准库模块 itertools 来救援。

大多数情况下,您会选择编写自定义生成器函数(使用yield),或更偶尔(当您需要真正复杂的状态管理时)自定义迭代器类< /em> (定义 __iter__ 特殊方法为 return self,以及 next [[or __next__最新版本的 Python]] 返回“迭代中的下一个值”。

捕获循环逻辑,而不是对循环本身按顺序生成的各种项目执行的任何操作,这是关键的抽象-这里有帮手!

Think iterators -- e.g., in this specific case:

for the_input in iter(lambda: raw_input('what to print?\n'), 'quit'):
    print the_input

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" -- the iter built-in can help (like here), sometimes genexps (generator expressions) can, sometimes the standard library module itertools 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 as return self, and next [[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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文