我想获取一个返回值,该值会由于触发的事件而改变值。 像这样:
class A:
def __init__(self):
self.ev_nearby = 0
def changeValue(self):
self.ev_nearby = 1
return self.ev_nearby
# I want this value in class B
def getChangedValue(self):
return self.ev_nearby
def logic(self):
while(somethingHappens) {
self.changeValue()
}
# logic is called in another class
class B:
A = A()
# I want the value here like
valueFromA = A.getChangedValue()
之所以如此,是因为我在服务器中动态需要两个值。 但是直到现在,我在类B中的valueFromA仍然保持为0(已触发或未触发)。
答案 0 :(得分:0)
似乎.logic()
在对象的另一个实例上被调用,而不是您尝试从中检索值的实例。要从同一实例检索值,可以将其作为变量传递给另一个类的范围。有关更多信息,请查看Object Oriented Programming (in Python)
下面,我写了一个小例子来说明如何实现:
class A:
def __init__(self):
self.ev_nearby = 0
def changeValue(self):
self.ev_nearby = 1
return self.ev_nearby
# I want this value in class B
def getChangedValue(self):
return self.ev_nearby
def logic(self):
while(somethingHappens) {
self.changeValue()
}
# logic is called in another class
class B:
# I want the value here like
def getValueFromA(self, A):
valueFromA = A.getChangedValue()
return valueFromA
class C:
A = A()
A.logic()
B = B()
B.getValueFromA(A)