2 回答

TA贡献1829条经验 获得超7个赞
你,可以使用python的@propery。它的工作方式类似于 getter,类似于 C# 和 JAVA 等其他语言。
在您的情况下,您可以((i, j), (notEqual(A[i], A[j])))使用 getter制作一个类似于对象的对象。
请参阅下面的示例实现
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

TA贡献1871条经验 获得超8个赞
不,没有办法。在大多数语言中,表达式是用变量的当前值计算的。not_equal(current_value_of_A, current_value_of_B)
当 A 和/或 B 的值发生变化时,您无法创建这样一种工作方式会自动更改的表达式。因此,以一种或另一种方式,您必须重新运行您的代码。
做类似事情的一种常见方法是观察者模式。也就是说,将您的表达式包装在一个类中,并在某些内容的值发生变化时通知该类。
除此之外,使用 dict 而不是具有形式的列表,{(i,j): notEqual(A[i], A[j])
因此您可以仅更新单个 (i, j) 对,而无需重新运行整个代码
添加回答
举报