while循环内的代码冗余小(感觉不太干净) [英] small code redundancy within while-loops (doesn't feel clean)

查看:138
本文介绍了while循环内的代码冗余小(感觉不太干净)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在Python中(尽管我认为它可以应用于多种语言),但我发现自己经常遇到这样的事情:

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")

也许我太挑剔了,但是我不喜欢重复行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
}

这绝对不是Python语言,并且Python甚至不允许在循环条件AFAIK中进行分配.

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

但也感觉不太正确. while 1表示此循环将永远运行;我正在使用一个循环,但是给它一个假的条件,然后将真正的条件放入其中.

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?

推荐答案

思考迭代器-例如,在这种情况下:

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

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

除最低级别的抽象外,Python中的大多数循环都最好在捕获了循环逻辑"的底层迭代器的帮助下以for循环的形式实现-内置的iter可以帮助实现(例如此处),有时genexps(生成器表达式)可以,有时标准库模块itertools可以提供帮助.

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.

大多数情况下,您会选择编写自定义的生成器函数(使用yield的代码),或者偶尔(当您需要非常复杂的状态管理时)编写自定义的 iterator类(将__iter__特殊方法定义为return self,并且将next [[或最新版本的Python中的__next__定义为]]以返回迭代中的下一个值).

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!

这篇关于while循环内的代码冗余小(感觉不太干净)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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