<bdo id='nFoIZ'></bdo><ul id='nFoIZ'></ul>

    <i id='nFoIZ'><tr id='nFoIZ'><dt id='nFoIZ'><q id='nFoIZ'><span id='nFoIZ'><b id='nFoIZ'><form id='nFoIZ'><ins id='nFoIZ'></ins><ul id='nFoIZ'></ul><sub id='nFoIZ'></sub></form><legend id='nFoIZ'></legend><bdo id='nFoIZ'><pre id='nFoIZ'><center id='nFoIZ'></center></pre></bdo></b><th id='nFoIZ'></th></span></q></dt></tr></i><div id='nFoIZ'><tfoot id='nFoIZ'></tfoot><dl id='nFoIZ'><fieldset id='nFoIZ'></fieldset></dl></div>

    1. <small id='nFoIZ'></small><noframes id='nFoIZ'>

        <legend id='nFoIZ'><style id='nFoIZ'><dir id='nFoIZ'><q id='nFoIZ'></q></dir></style></legend>
      1. <tfoot id='nFoIZ'></tfoot>

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

        时间:2023-10-09

            <small id='8v5oA'></small><noframes id='8v5oA'>

              <tbody id='8v5oA'></tbody>
          • <i id='8v5oA'><tr id='8v5oA'><dt id='8v5oA'><q id='8v5oA'><span id='8v5oA'><b id='8v5oA'><form id='8v5oA'><ins id='8v5oA'></ins><ul id='8v5oA'></ul><sub id='8v5oA'></sub></form><legend id='8v5oA'></legend><bdo id='8v5oA'><pre id='8v5oA'><center id='8v5oA'></center></pre></bdo></b><th id='8v5oA'></th></span></q></dt></tr></i><div id='8v5oA'><tfoot id='8v5oA'></tfoot><dl id='8v5oA'><fieldset id='8v5oA'></fieldset></dl></div>
              <bdo id='8v5oA'></bdo><ul id='8v5oA'></ul>
                <legend id='8v5oA'><style id='8v5oA'><dir id='8v5oA'><q id='8v5oA'></q></dir></style></legend><tfoot id='8v5oA'></tfoot>
                  本文介绍了如何触发价值变化的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  I realise this question has to do with event-handling and i've read about Python event-handler a dispatchers, so either it did not answer my question or i completely missed out the information.

                  我希望在值 v 发生变化时触发对象 A 的方法 m():

                  I want method m() of object A to be triggered whenever value v is changing:

                  例如(假设金钱使人快乐):

                  For instance (assuming money makes happy):

                  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
                  

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

                  So whenever the global_wealth value is changed, all instances of the class Person should change their happiness value accordingly.

                  NB:我不得不编辑这个问题,因为第一个版本似乎表明我需要 getter 和 setter 方法.很抱歉造成混乱.

                  NB: I had to edit the question since the first version seemed to suggest i needed getter and setter methods. Sorry for the confusion.

                  推荐答案

                  你需要使用 观察者模式.在以下代码中,一个人订阅接收来自全球财富实体的更新.当全球财富发生变化时,该实体会提醒其所有订阅者(观察者)发生变化.Person 然后更新自己.

                  You need to use the Observer Pattern. In the following code, a person subscribes to receive updates from the global wealth entity. When there is a change to global wealth, this entity then alerts all its subscribers (observers) that a change happened. Person then updates itself.

                  我在这个例子中使用了属性,但它们不是必需的.一个小警告:属性仅适用于新样式类,因此类声明后的 (object) 是强制性的.

                  I make use of properties in this example, but they are not necessary. A small warning: properties work only on new style classes, so the (object) after the class declarations are mandatory for this to work.

                  class GlobalWealth(object):
                      def __init__(self):
                          self._global_wealth = 10.0
                          self._observers = []
                  
                      @property
                      def global_wealth(self):
                          return self._global_wealth
                  
                      @global_wealth.setter
                      def global_wealth(self, value):
                          self._global_wealth = value
                          for callback in self._observers:
                              print('announcing change')
                              callback(self._global_wealth)
                  
                      def bind_to(self, callback):
                          print('bound')
                          self._observers.append(callback)
                  
                  
                  class Person(object):
                      def __init__(self, data):
                          self.wealth = 1.0
                          self.data = data
                          self.data.bind_to(self.update_how_happy)
                          self.happiness = self.wealth / self.data.global_wealth
                  
                      def update_how_happy(self, global_wealth):
                          self.happiness = self.wealth / global_wealth
                  
                  
                  if __name__ == '__main__':
                      data = GlobalWealth()
                      p = Person(data)
                      print(p.happiness)
                      data.global_wealth = 1.0
                      print(p.happiness)
                  

                  这篇关于如何触发价值变化的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:哪些 Python 包提供独立的事件系统? 下一篇:在 Python 中从同一类中的另一个调用一个方法

                  相关文章

                  最新文章

                    <bdo id='xI4ns'></bdo><ul id='xI4ns'></ul>
                  <i id='xI4ns'><tr id='xI4ns'><dt id='xI4ns'><q id='xI4ns'><span id='xI4ns'><b id='xI4ns'><form id='xI4ns'><ins id='xI4ns'></ins><ul id='xI4ns'></ul><sub id='xI4ns'></sub></form><legend id='xI4ns'></legend><bdo id='xI4ns'><pre id='xI4ns'><center id='xI4ns'></center></pre></bdo></b><th id='xI4ns'></th></span></q></dt></tr></i><div id='xI4ns'><tfoot id='xI4ns'></tfoot><dl id='xI4ns'><fieldset id='xI4ns'></fieldset></dl></div>

                • <legend id='xI4ns'><style id='xI4ns'><dir id='xI4ns'><q id='xI4ns'></q></dir></style></legend>

                  1. <tfoot id='xI4ns'></tfoot>

                    1. <small id='xI4ns'></small><noframes id='xI4ns'>