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

发布于 2024-08-12 02:19:35 字数 415 浏览 4 评论 0原文

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

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

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

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

有人有什么建议吗?

更新:

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

It seems that Python has some limitations regarding instance methods.

  1. Instance methods can't be copied.
  2. Instance methods can't be pickled.

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.

Does anyone have any suggestions?

Update:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

看轻我的陪伴 2024-08-19 02:19:35

您可以使用 copy_reg.pickle< 来完成此操作/a>.在 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)

这不存储方法的代码,只存储其名称;但这在常见情况下可以正常工作。

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

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!

疯到世界奔溃 2024-08-19 02:19:35

REST - 表示状态转移。只发送状态,不发送方法。

要将对象 X 从 A 传输到 B,我们这样做。

  1. A 在某些情况下对 X 的状态进行编码
    方便、易于解析的符号。 JSON
    很流行。

  2. A 将 JSON 文本发送给 B。

  3. B 将 X 的状态解码为 JSON
    符号,重建X。

必须具有 X 类的类定义才能正常工作。 B 必须具有 X 的类所依赖的所有函数和其他类定义。简而言之,两者都是A
和 B 具有所有定义。仅移动对象状态的表示
大约。

请参阅任何有关 REST 的文章。

http://en.wikipedia.org/wiki/Representational_State_Transfer

http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

REST - Representation State Transfer. Just send state, not methods.

To transfer an object X from A to B, we do this.

  1. A encode the state of X in some
    handy, easy-to-parse notation. JSON
    is popular.

  2. A sends the JSON text to B.

  3. B decodes the state of X form JSON
    notation, reconstructing X.

B must have the class definitions for X's class for this to work. B must have all functions and other class definitions on which X's class depends. In short, both A
and B have all the definitions. Only a representation of the object's state gets moved
around.

See any article on REST.

http://en.wikipedia.org/wiki/Representational_State_Transfer

http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm

情深如许 2024-08-19 02:19:35

pickle 实例,然后在 unpickle 后访问该方法。 Pickling 实例的方法没有意义,因为它依赖于实例。如果没有,那就写成一个独立的函数。

import pickle

class A:
     def f(self):
         print 'hi'

x = A()
f = open('tmp', 'w')
r = pickle.dump(x, f)
f.close()
f = open('tmp', 'r')
pickled_x = pickle.load(f)
pickled_x.f()

pickle the instance and then access the method after unpickling it. Pickling a method of an instance doesn't make sense because it relies on the instance. If it doesn't, then write it as an independent function.

import pickle

class A:
     def f(self):
         print 'hi'

x = A()
f = open('tmp', 'w')
r = pickle.dump(x, f)
f.close()
f = open('tmp', 'r')
pickled_x = pickle.load(f)
pickled_x.f()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文