简体   繁体   English

为什么python不支持:while(s = f.readline())?

[英]why python not supporting : while( s = f.readline())?

more example below: 更多示例如下:

 if (a = xyz() ) > abc: 

Really want to know why, is it kinds of bad smell ? 真想知道为什么,难道是难闻的种类?

Note: I know the grammar, but I am asking why Python don't support such grammar as many other language( eg, c , java...) do supporting 注意:我知道语法,但我问为什么Python不支持这样的语法,因为许多其他语言(例如,c,java ...)支持

From the python design FAQ : python设计FAQ

The reason for not allowing assignment in Python expressions is a common, hard-to-find bug in those other languages, caused by this construct: 不允许在Python表达式中赋值的原因是由这个构造引起的其他语言中常见的,难以发现的错误:

  if (x = 0) { // error handling } else { // code that only works for nonzero x } 

The error is a simple typo: x = 0, which assigns 0 to the variable x, was written while the comparison x == 0 is certainly what was intended. 错误是一个简单的错字:x = 0,它将0赋给变量x,而比较x == 0肯定是预期的。

As explained in the FAQ, most "use cases" for using assignment in an expression can be covered by using iterators instead. 正如FAQ中所解释的那样,使用迭代器可以覆盖在表达式中使用赋值的大多数“用例”。

Remember python philosophy : There should be one-- and preferably only one --obvious way to do it. 记住python哲学:应该有一个 - 最好只有一个 - 明显的方法来做到这一点。
It doesn't sound obvious to me ! 这对我来说听起来不太明显!
Also you may want to check this answer too : 您也可以查看此答案:
why does python assignment not return a value 为什么python赋值不返回值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM