The reason for not allowing assignment in Python expressions is a common, hard-to-find bug in those other languages.
Many alternatives have been proposed. Most are hacks that save some typing but use arbitrary or cryptic syntax or keywords, and fail the simple criterion for language change proposals: it should intuitively suggest the proper meaning to a human reader who has not yet been introduced to the construct.
An interesting phenomenon is that most experienced Python programmers recognize the while True idiom and don’t seem to be missing the assignment in expression construct much; it’s only newcomers who express a strong desire to add this to the language.
There’s an alternative way of spelling this that seems attractive:
line = f.readline() while line:
... # do something with line...
line = f.readline()
不,对不起。这是一个常见问题,在这里解释得很好:
在Pydocs,和Fredrik Lundh's blog。
您可以将
raw_input()
包装成生成器:这意味着我们回到了原点,但代码更复杂。因此,如果需要包装现有方法,则需要使用nosklo的方法。
或者如果你想要一个列表:
相关问题 更多 >
编程相关推荐