相关疑难解决方法(0)

如何触发价值变化的功能?

我意识到这个问题与事件处理有关,我已经阅读了关于Python事件处理程序的调度程序,所以要么它没有回答我的问题,要么我完全错过了这些信息.

我想要在值发生变化时触发m()对象的方法:Av

例如(假设钱让人开心):

global_wealth = 0

class Person()
    def __init__(self):
        self.wealth = 0
        global global_wealth
        # here is where attribute should be
        # bound to changes in 'global_wealth'
        self.happiness = bind_to(global_wealth, how_happy)

    def how_happy(self, global_wealth):
        return self.wealth / global_wealth
Run Code Online (Sandbox Code Playgroud)

因此,每当global_wealth更改值时,类的所有实例Person都应相应地更改其happiness值.

注意:我必须编辑问题,因为第一个版本似乎暗示我需要getter和setter方法.对困惑感到抱歉.

python event-handling observer-pattern

48
推荐指数
3
解决办法
6万
查看次数

python检测字典中的任何元素是否发生更改

而不是保存字典的副本并比较旧的和新的,类似于:

dict = { "apple":10, "pear":20 }

if ( dict_old != dict ):
   do something
   dict_old = dict
Run Code Online (Sandbox Code Playgroud)

如何检测词典的任何元素何时发生变化?

python dictionary

8
推荐指数
2
解决办法
7031
查看次数