简体   繁体   English

更改某些变量的值后更改条件

[英]Change a condition after changing the value of some variables

i have this situation: a list of variables initially set to none A = [none, none, none, none] and a very simple function that controls if 2 values (different to none) are different: 我有这种情况:最初设置为无的变量列表A = [none, none, none, none]和一个非常简单的函数来控制2个值(与无)是否不同:

def notEqual(a, b):
   if a is None and b is None:
       return True
   if a != b:
       return True
   return False

I want create a list named bigList that shows for each couple of element of A if they are equal or not. 我想创建一个名为bigList的列表,该列表为A的每个元素相等或不相等的元素显示。 So i thought to do this: 所以我想这样做:

for i in range(4):
   for j in range(4):
      if i != j:
         c = ((i, j), (notEqual(A[i], A[j])))
         bigList.append((c))

So at the beginning all the element of bigList are ((i,j), (True)) In a second moment i have this situation: 因此,一开始bigList的所有元素都是((i,j),(True))在第二时刻我遇到了这种情况:

A[0]=10 A[1]=10

So the condition associated to (0,1) and (1,0) have to change to False. 因此,与(0,1)和(1,0)相关的条件必须更改为False。 Is there an easy way to do something like this? 有一种简单的方法可以执行这样的操作吗? (Change some conditions when some variables change their values) (当某些变量更改其值时,请更改某些条件)

No, there is no way. 不,没有办法。 In most languages, expression are evaluated with current values of the variables. 在大多数语言中,表达式是用变量的当前值求值的。 You can't make an expression that works like not_equal(current_value_of_A, current_value_of_B) that will automatically change when values of A and/or B change. 您不能创建像not_equal(current_value_of_A, current_value_of_B)这样的表达式,该表达式将在A和/或B的值更改时自动更改。 So, in one way or another, you have to re-run your code. 因此,您必须以某种方式重新运行代码。

A common way of doing something similar is the observer pattern . 做类似事情的常见方式是观察者模式 That is, wrap your expression in a class and notify the class when the value of something changes. 也就是说,将您的表达式包装在一个类中,并在某些值更改时通知该类。

Along with that, use a dict instead of a list, which has the form {(i,j): notEqual(A[i], A[j]) , so you can update only the individual (i, j) pair without re-running your whole code 除此之外,使用字典而不是列表,列表的形式为{(i,j): notEqual(A[i], A[j]) ,因此您可以仅更新单个(i,j)对而不使用重新运行您的整个代码

You, can use @propery of python. 您可以使用python的@propery It works like getter similar to other languages like C# and JAVA. 它的工作方式类似于类似其他语言(如C#和JAVA)的getter。

In your case you can make a object similar to that of ((i, j), (notEqual(A[i], A[j]))) with a getter. 在您的情况下,可以使用吸气剂制造与((i, j), (notEqual(A[i], A[j])))相似的对象。

See sample implementation below 请参阅下面的示例实现

class bigListElement(object):
    A = [] # this is static
    def __init__(self, index_tuple, areEqual):
        self.index_tuple = index_tuple
        self._areEqual = areEqual

    @staticmethod
    def notEqual(a, b):
        if a is None and b is None:
            return True
        if a != b:
            return True
        return False

    @property
    def areEqual(self):
        return bigListElement.notEqual(bigListElement.A[self.index_tuple[0]], bigListElement.A[self.index_tuple[1]])


print("Hello World")

bigListElement.A = [10, 2, 10, 3, 4]
a = bigListElement((0, 1), False)

print(a.areEqual) # True

bigListElement.A[1] = 10

print(a.areEqual) # False

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

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