简体   繁体   English

确定if条件中使用的布尔表达式中哪个条件为真

[英]Determine which condition is true in a boolean expression being used in if condition

Suppose we have a simple boolean expression filled with boolean return type function calls with logical OR operators between them. 假设我们有一个简单的boolean表达式,其中填充了布尔返回类型函数调用,并且它们之间具有logical OR运算符。 This boolean expression is used only in a single if clause. boolean表达式仅在单个if子句中使用。 For example: 例如:

if (c1() or c2() or c3()): #c1(), c2(), c3() are sample boolean return type methods
  foo1()
  foo2()
  print ("foo1() and foo2() have been executed because condition # <number> is true")
  1. Is it possible to find out the method call (most basic unit of condition such as c2() is condition # 2) because of which the program entered the code inside the if clause without storing the return values of any of the method call or calling any method again? 是否有可能找出方法调用(例如c2()类的最基本条件单元是条件#2),因此程序在if子句中输入了代码,而没有存储任何方法调用或调用的返回值再有办法吗?
  2. Suppose c1() , c2() and c3() would have all returned true boolean values. 假设c1()c2()c3()都将返回true布尔值。 However due to short-circuit boolean evaluation , c2() and c3() would never be executed. 但是,由于short-circuit boolean evaluation ,将永远不会执行c2()c3() Can we somehow find it because of which conditions/method calls, the program might have still entered the code block inside the if clause while applying the same conditions mentioned in point number 1. 我们可以通过某种条件/方法调用以某种方式找到它吗,在应用第1点中提到的相同条件时,程序可能仍在if子句中输入了代码块。

The answer can be in any programming language. 答案可以是任何编程语言。 I'm just curious to see if it is and how it is possible. 我只是想知道它是否可行以及如何实现。

For some reason you tagged both on python and c# , the answer can be completly different in each language. 由于某些原因,您同时在pythonc#上进行了标记,每种语言的答案可能完全不同。

In c# I would have done it in the following pattern: 在c#中,我会按照以下模式进行操作:

public ResultModel checkSomething()
{
    ResultModel retVal = new ResultModel();

    if(condition1)
    {
        retVal.DidSucceed = true;
        retVal.Source = eSource.Condition1;
    }
    else if (condiction2){
        ...
    }

    return retVal;
}

class ResultModel
{
    public bool DidSucceed {get;set;}
    public eSource Source {get;set;}
}

Here's how you can do this in Python, 这是您可以在Python中执行此操作的方法,

condn = 0
for i, c in enumerate((c1(), c2(), c3()):
    if c:
        condn = i + 1
        break
if condn:
  foo1()
  foo2()
  print("condition number #{} is true".format(condn))

# if c2() is true, it prints
"condition number #2 is true"

you can do something tricky like this, but not the best solution 您可以像这样做一些棘手的事情,但不是最好的解决方案

check = []
def c1():
    global check
    if True: check.append('c1')

第一个问题的答案是“否”,每当其中一个函数返回true ,if中的代码就会执行,并且如果没有调试器或将值存储在某个变量中就看不到值是什么。

To get this behaviour without polluting namespace with global variables or turning c* functions into classes I would implement it like this: 为了获得这种行为而不用全局变量污染名称空间或将c*函数转换为类,我将这样实现:

conditions = [c1, c2, c3]
for index, cond in enumerate(conditions):
    if cond():
        foo1()
        foo2()
        print(f"foo1() and foo2() have been executed because condition {index} is true")
        break

this basically hardcodes or behaviour, but other than that is fine. 这基本上是硬编码or行为,但除此之外没关系。

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

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