简体   繁体   English

检查多个条件并避免Python中的代码重复

[英]Checking multiple conditions and avoiding code duplication in Python

This is a 3-way conditional filtering happening inside a loop. 这是在循环内发生的3向条件过滤。 Here n must definitely meet one among the three conditions; 在这里,n必须满足三个条件中的一个; it must be zero, negative or a positive number. 它必须是零,负数或正数。 Function *dothis()* is repeated twice in the code (for zero or negative). 函数* dothis()*在代码中重复两次(零或负)。 Since one requires continuing the loop and another requires to break it, we can't group the conditions either. 由于一个需要继续循环而另一个需要打破它,我们也不能对这些条件进行分组。 The third one is filtered last and also needs to break the loop but after executing a different function *dothat()*. 第三个是最后过滤的,也需要打破循环但是在执行不同的函数* dothat()*之后。

I couldn't find another way, which would be much more elegant, to do this. 我找不到另一种方式,这样做会更优雅。 These function calls, if replaced by a series of statements would make it look even worse (which is happening in my real code). 这些函数调用,如果被一系列语句取代,会使它看起来更糟(这在我的实际代码中发生)。 Are there any possible workarounds? 有没有可能的解决方法?

while True:
    if n == 0:
        dothis()
        continue
    if n < 0:
        dothis()
        break
    dothat()
    break

Note: it is essential that all these must be inside the loop. 注意:所有这些必须在循环内部是至关重要的。

while True:
    if n <= 0:
        dothis()
        if n == 0: continue
    dothat()
    break

Assuming dothis() is actually the same function in both cases. 假设dothis()在两种情况下实际上都是相同的函数。 This would produce the same outcome. 这将产生相同的结果。

You're likely to get alot of stylistic/preference recommendations (and one person who will point out your test for equality is flawed -- you used assignment rather than equality). 你很可能会得到很多风格/偏好推荐(而且一个人会指出你的测试是否存在缺陷 - 你使用的是赋值而不是相等)。 I think in the long run, it's what ever you feel is simpler to understand. 我认为从长远来看,这是你感觉更容易理解的东西。

My personal favorite is: 我个人最喜欢的是:

while n == 0:
    dothis()
if n < 0:
    dothis()
elif n:
    dothat()

It might be better to separate your decision-points: 1) do I do this or that, and 2) do I stop here? 分离你的决策点可能更好:1)我是这样做还是那样,2)我是否会在这里停下来?

while True:
    if n > 0:
        dothat()
    else:
        dothis()

    if n:
        break

I personally prefer to separate code into logical sections. 我个人更喜欢将代码分成逻辑部分。 If it can only happen once, then only write it once. 如果它只能发生一次,那么只写一次。 Although this way isn't as compact/ doesn't look as pretty, I find that it's more obvious what will happen. 虽然这种方式并不紧凑/看起来不漂亮,但我发现更明显的是会发生什么。 When I (or someone else) glances at the code I can say.. "Okay it will either dothis() or dothat() then break or continue ." 当我(或其他人)浏览代码时,我可以说...... “好吧,无论是dothis()还是dothat()然后breakcontinue 。”

Assuming in this case that n is not changed by dothis() or dothat() 假设在这种情况下, n不会被dothis()dothat()

 while True:
    # How to call
    if n <= 0:
        dothis()
    else:
        dothat()
    # How to end
    if n == 0:
        continue
    else:
        break

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

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