Alifs看起来很邪恶,我无法轻易推断出控制流程,但是 那么它们是一个新想法。我会对那个人保持开放的态度。 我认为最好的方法是将the / else语法与 相同的功能进行比较现在看起来像是在python中[暗示某人现在比我更多时间的时间;)]。我会投票给两个更简洁,更可读的两个。 -Dan Ron Adam写道:它发生在我身上(几个星期前,他试图寻找形成if-elif-else块的最佳方法,在非常一般的层面上,''也''声明可能是有用的。所以我想知道其他人会怎么想它。 for x in< iteriable>: BLOCK1 如果< condition> ;: break #do else block 还: BLOCK2 否则: BLOCK3 对于这个特定的情况,你可以重写 当前Python中的代码 for x in< iterable>: BLOCK1 如果< condition>: BLOCK3 break else: BLOCK2 为了使你的建议有用,你需要一个 的例子,如下所示当前的Python for x in< iterable>: ... if< condition>: BLOCK3 休息 ... 如果< condition>: BLOCK3 休息 否则: BLOCK2 也就是说,BLOCK3; break在循环中多次出现 。我的直觉是,这并不经常发生,足以需要一种新的语法来简化它。 你能指出一些现有的代码吗?改善 还带你的/其他? 而< condition1>: BLOCK1 如果< condition2>:break#jump to else 另外: BLOCK2 否则: BLOCK3 这里如果while循环结束于while< condition1> ;,那么BLOCK2 执行,或者如果执行了中断,BLOCK3执行。 与 相同(在当前的Python中)< condition>: BLOCK1 if< condition2>: BLOCK3 break else: BLOCK2 In和if声明...... 如果< condition1>: BLOCK1 elif< condition2>: BLOCK2 elif < condition3>: BLOCK3 还: BLOCK4 否则: BLOCK5 这里,如果有的话还会执行块条件为真,否则else块将执行。 那是......时髦。什么时候有用? 一个或许是hackish解决方案我已经为罕见的情况做了什么 我认为你的建议很有用 而1: 如果< condition1>: BLOCK1 elif< condition2>: BLOCK2 elif< condition3>: BLOCK3 else: #couldn什么都不做 休息 BLOCK4 休息 我觉得这给了Pythons一般的流量控制一些不错对称的和一贯的特征似乎对我很有吸引力。任何人都同意吗? 否。有更多的方法来控制流程并不能代表那些' 易于阅读。 在思考(或听到)关于新的Python 语言更改之后我通常的下一步是查看现有代码并查看是否如果这是一个常见或罕见的问题,现有的代码会更容易阅读/理解并获得 的想法。也许你可以指出 这几行的例子吗? Andrew da *** @ dalkescientific.com Eloff写道:我的第一反应是这太可怕了,否则循环上的条款足够令人困惑。但是,如果我更多地考虑它,我正在热衷于这个想法。此外/其他循环是清晰,对称,并且将是有用的。 反转其他的手段将破坏代码,但它不经常使用,它是一个令人困惑的事情开始时,没有什么不对,如果它开始时被打破了一些东西(慢慢地!)。 Alifs看起来很邪恶,我无法推断很容易控制流程,但是这是一个新想法。我会对那个保持开放的态度。 是的,这可能是因为它是新的。 Alifs将在你想要的地方使用 来测试多个可能的值,并且需要以不同的方式响应 ,具体取决于值。它们可能不会像精灵那样频繁使用。 alifs是一种串联方式,如果'作为一个整体组合在一起。以下将 等同于if-alif-also-else。 didif = False 如果val == condition1 : didif = True BLOCK1 如果val == condition2: didif = True BLOCK2 如果val == condition3: didif = True if didif: BLOCK3 其他: BLOCK4 if-alif-also-else版本不需要额外的名称来标记是否有任何 的条件是真的。 如果val == condition1: BLOCK1 alif val = =条件2: BLOCK2 alif val == condition3: BLOCK3 还: BLOCK4 否则: BLOCK5 但我想我们需要找一些真实的用例''s使它成为 令人信服。 我认为最好的方法是将also / else语法与相同的功能相比较现在是python [暗示现在比我更有时间的人;)]。我会投票支持两者中更简洁,更可读的内容。 -Dan 如果是if-also,等效代码需要一个额外的局部变量 和一个额外的if语句。 iftest = False if < condition>: iftest = True BLOCK1 elif< condition>: iftest = True BLOCK2 if iftest: BLOCK3 这是让我思考的模式有一个也。我当时正在解析和格式化doc字符串,并且还允许它成为。 如果<条件>: BLOCK1 elif< condition>: BLOCK2 还: BLOCK3 哪个更容易阅读。 RonThere seems to be a fair amount of discussion concerning flow controlenhancements lately. with, do and dowhile, case, etc... So here''s myflow control suggestion. ;-)It occurred to me (a few weeks ago while trying to find the best way toform a if-elif-else block, that on a very general level, an ''also''statement might be useful. So I was wondering what others would thinkof it.''also'' would be the opposite of ''else'' in meaning. And in the case of afor-else... would correct what I believe to be an inconsistency.Currently the else block in a for loop gets executed if the loop iscompleted, which seems backwards to me. I would expect the else tocomplete if the loop was broken out of. That seems more constant withif''s else block executing when if''s condition is false.''also'' would work like this.for x in <iteriable>:BLOCK1if <condition>: break # do else blockalso:BLOCK2else:BLOCK3where the also block is executed if the loop completes (as the currentelse does), and the else block is executed if it doesn''t.while <condition1>:BLOCK1if <condition2>: break # jump to elsealso:BLOCK2else:BLOCK3Here if the while loop ends at the while <condition1>, the BLOCK2executes, or if the break is executed, BLOCK3 executes.In and if statement...if <condition1>:BLOCK1elif <condition2>:BLOCK2elif <condition3>:BLOCK3also:BLOCK4else:BLOCK5Here, the also block would execute if any previous condition is true,else the else block would execute.And ''tentatively''<g>, there is the possible ''alif'', (also-if), to beused this way.if <condition1>:BLOCK1alif <condition2>:BLOCK2break # skip to also blockalif <condition3>:BLOCK3alif <condition4>:BLOCK4also: # at lease one condition was trueBLOCK5else: # no conditions evaluated as trueBLOCK6Where only one elif will ever evaluate as true, any or all ''alif''s couldevaluate as true. This works similar to some case statements except allalif conditions may be evaluated. Not a true case statement, but a goodreplacement where speed isn''t the main concern?I''m not sure if ''elif''s and ''alif''s could or should be used together?Maybe a precedence rule would be needed. Or simple that all also''s andalif''s must precede any elif''s. Then again maybe alif''s between elif''scould be useful? For example the alif <condition2> could be and ''elif''instead, so the break above would''t be needed.''also'' can be used without the else in the above examples, and of coursethe ''else'' doesn''t need an ''also''.I think this gives Pythons general flow control some nice symmetricaland consistent characteristics that seem very appealing to me. Anyoneagree?Any reason why this would be a bad idea? Is there an equivalent to analso in any other language? (I haven''t seen it before.)Changing the for-else is probably a problem... This might be a 3.0 wishlist item because of that.Is alif too simular to elif?On the plus side, I think this contributes to the pseudocode characterof Python very well.Cheers, Ron 解决方案 My first reaction was that this is terrible, else clauses on loops areconfusing enough. But if I think about it more, I''m warming up to theidea. Also/Else for loops is clear, symmetrical, and would be useful.Reversing the meanign of else will break code, but it''s not used thatfrequently, and it was a confusing thing to begin with, nothing wrongin breaking something (slowly!) if it was ''broken'' to begin with.Alifs look evil, I couldn''t deduce the control flow very easily, butthen they are a new idea. I''ll keep an open mind on that one.I think the best thing would be to compare the also/else syntax to whatidentical functionality looks like in python now [hint to someone withmore time than me right now ;)]. I''d vote for whichever is the moreconcise and readable of the two.-Dan Ron Adam wrote: It occurred to me (a few weeks ago while trying to find the best way to form a if-elif-else block, that on a very general level, an ''also'' statement might be useful. So I was wondering what others would think of it. for x in <iteriable>: BLOCK1 if <condition>: break # do else block also: BLOCK2 else: BLOCK3For this specific case you could rewrite the code incurrent Python asfor x in <iterable>:BLOCK1if <condition>:BLOCK3breakelse:BLOCK2In order for your proposal to be useful you would need anexample more like the following in current Pythonfor x in <iterable>:...if <condition>:BLOCK3break...if <condition>:BLOCK3breakelse:BLOCK2That is, where "BLOCK3;break" occurs multiple times inthe loop. My intuition is that that doesn''t occur oftenenough to need a new syntax to simplify it.Can you point to some existing code that would be improvedwith your also/else? while <condition1>: BLOCK1 if <condition2>: break # jump to else also: BLOCK2 else: BLOCK3 Here if the while loop ends at the while <condition1>, the BLOCK2 executes, or if the break is executed, BLOCK3 executes.which is the same (in current Python) aswhile <condition>:BLOCK1if <condition2>:BLOCK3breakelse:BLOCK2 In and if statement... if <condition1>: BLOCK1 elif <condition2>: BLOCK2 elif <condition3>: BLOCK3 also: BLOCK4 else: BLOCK5 Here, the also block would execute if any previous condition is true, else the else block would execute.That is ... funky. When is it useful?One perhaps hackish solution I''ve done for the rare cases whenI think your proposal is useful iswhile 1:if <condition1>:BLOCK1elif <condition2>:BLOCK2elif <condition3>:BLOCK3else:# couldn''t do anythingbreakBLOCK4break I think this gives Pythons general flow control some nice symmetrical and consistent characteristics that seem very appealing to me. Anyone agree?No. Having more ways to do control flow doesn''t make for code that''seasy to read.My usual next step after thinking (or hearing) about a new Pythonlanguage change is to look at existing code and see if there''sexisting code which would be easier to read/understand and get anidea if it''s a common or rare problem. Perhaps you could pointout a few examples along those lines?Andrew da***@dalkescientific.com Eloff wrote: My first reaction was that this is terrible, else clauses on loops are confusing enough. But if I think about it more, I''m warming up to the idea. Also/Else for loops is clear, symmetrical, and would be useful. Reversing the meanign of else will break code, but it''s not used that frequently, and it was a confusing thing to begin with, nothing wrong in breaking something (slowly!) if it was ''broken'' to begin with. Alifs look evil, I couldn''t deduce the control flow very easily, but then they are a new idea. I''ll keep an open mind on that one.Yes, it''s probably because it''s new. Alifs would be used where you wantto test for multiple possible values, and need to respond differentlydepending on the values. They probably wouldn''t be used as often as elifs.alifs is a way to string if''s together as a group. The following wouldbe equivalent to if-alif-also-else.didif = Falseif val == condition1:didif = TrueBLOCK1if val == condition2:didif = TrueBLOCK2if val == condition3:didif = Trueif didif:BLOCK3else:BLOCK4The if-alif-also-else version doesn''t need the extra name to mark if anyof the conditions were true.if val == condition1:BLOCK1alif val == condition2:BLOCK2alif val == condition3:BLOCK3also:BLOCK4else:BLOCK5But I think we will need to find some real use case''s to make itconvincing. I think the best thing would be to compare the also/else syntax to what identical functionality looks like in python now [hint to someone with more time than me right now ;)]. I''d vote for whichever is the more concise and readable of the two. -DanIn cases of if-also, the equivalent code needs an extra local variableand an additional if statement.iftest = Falseif <condition>:iftest = TrueBLOCK1elif <condition>:iftest = TrueBLOCK2if iftest:BLOCK3This is the pattern that caused me to think of having an also. I wasparsing and formatting doc strings at the time, and also would allow itto become.if <condition>:BLOCK1elif <condition>:BLOCK2also:BLOCK3Which is much easier to read.Ron 这篇关于&QUOT;也&QUOT;平衡“其他” ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 11:14
查看更多