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

        • <bdo id='5RBSU'></bdo><ul id='5RBSU'></ul>

      1. <small id='5RBSU'></small><noframes id='5RBSU'>

        <legend id='5RBSU'><style id='5RBSU'><dir id='5RBSU'><q id='5RBSU'></q></dir></style></legend>

        克服 Python 关于实例方法的限制

        Overcoming Python#39;s limitations regarding instance methods(克服 Python 关于实例方法的限制)

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

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

          <tfoot id='hpyBF'></tfoot>
            <bdo id='hpyBF'></bdo><ul id='hpyBF'></ul>
                <legend id='hpyBF'><style id='hpyBF'><dir id='hpyBF'><q id='hpyBF'></q></dir></style></legend>

                  本文介绍了克服 Python 关于实例方法的限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  似乎 Python 在实例方法方面有一些限制.

                  It seems that Python has some limitations regarding instance methods.

                  1. 无法复制实例方法.
                  2. 实例方法不能被pickle.

                  这对我来说是个问题,因为我在一个非常面向对象的项目中工作,我在其中引用了实例方法,并且使用深度复制和酸洗.酸洗的事情主要是由多处理机制来完成的.

                  This is problematic for me, because I work on a very object-oriented project in which I reference instance methods, and there's use of both deepcopying and pickling. The pickling thing is done mostly by the multiprocessing mechanism.

                  什么是解决这个问题的好方法?我对复制问题做了一些丑陋的解决方法,但是我正在为这两个问题寻找更好的解决方案.

                  What would be a good way to solve this? I did some ugly workaround to the copying issue, but I'm looking for a nicer solution to both problems.

                  有人有什么建议吗?

                  更新:

                  我的用例:我有一个很小的事件系统.每个事件都有一个 .action 属性,该属性指向它应该触发的函数,有时该函数是某个对象的实例方法.

                  My use case: I have a tiny event system. Each event has an .action attribute that points to a function it's supposed to trigger, and sometimes that function is an instance method of some object.

                  推荐答案

                  您也许可以使用 copy_reg.pickle.在 Python 2.6 中:

                  You might be able to do this using copy_reg.pickle. In Python 2.6:

                  import copy_reg
                  import types
                  
                  def reduce_method(m):
                      return (getattr, (m.__self__, m.__func__.__name__))
                  
                  copy_reg.pickle(types.MethodType, reduce_method)
                  

                  这不存储方法的代码,只是它的名字;但这在常见情况下可以正常工作.

                  This does not store the code of the method, just its name; but that will work correctly in the common case.

                  这使得酸洗和复制都可以工作!

                  This makes both pickling and copying work!

                  这篇关于克服 Python 关于实例方法的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                • <small id='POVEC'></small><noframes id='POVEC'>

                    <tbody id='POVEC'></tbody>

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

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