如何挑选Python类方法类型(即多处理)?

时间:2015-02-20 00:17:44

标签: python python-2.7 python-multiprocessing pickle

如何挑选Python类方法类型?它没有在标准库中实现。我发现我可以使用一些第三方模块,例如dill,但是如何只使用pickle。

我准备了一些测试代码来简化这个问题:

import pickle

class Thing(object):
  @staticmethod
  def static_method():
    print Thing.static_method.__name__

  @classmethod
  def class_method(cls):
    print cls.class_method.__name__

  def object_method(self):
    print self.object_method.__name__


def main():
  items = [Thing,
           Thing(),
           Thing.static_method,
           Thing.class_method,
           Thing.object_method,
           Thing().object_method]
  for i, item in enumerate(items):
    try:
      pickle.loads(pickle.dumps(item))
    except Exception as ex:
      print i, ex, item

if __name__ == '__main__':
  main()

如何处理Python 2.7中方法缺省的pickle支持?

2 Can't pickle <function static_method at 0x025614F0>: it's not found as __main__.static_method <function static_method at 0x025614F0>
3 can't pickle instancemethod objects <bound method type.class_method of <class '__main__.Thing'>>
4 can't pickle instancemethod objects <unbound method Thing.object_method>
5 can't pickle instancemethod objects <bound method Thing.object_method of <__main__.Thing object at 0x024F71D0>>

0 个答案:

没有答案