gpt4 book ai didi

python - 如何将类装饰器与pickle一起使用?

转载 作者:行者123 更新时间:2023-11-30 23:02:57 24 4
gpt4 key购买 nike

我想使用类装饰器(而不是函数装饰器!),例如

def class_decorator(cls):
class new_cls(cls):
def run(self, *args, **kwargs):
print 'In decorator'
super(new_cls,self).run(*args, **kwargs)
return new_cls

@class_decorator
class cls(object):
'''
a class
'''
def run(self):
print 'called'

并且能够 pickle 对象:

import pickle

a = cls()
a.run()
s = pickle.dumps(a)

但是 pickle 返回错误:

PicklingError: Can't pickle <class '__main__.new_cls'>: it's not found as __main__.new_cls

任何帮助将不胜感激!

最佳答案

当你pickle一个类时,the name of the class -- not its value -- ispickled 。如果class_decorator返回一个新类,其名称未在模块的顶层,然后你会得到错误:

PicklingError: Can't pickle <class '__main__.new_cls'>: it's not found as __main__.new_cls 

您可以通过将新的修饰类命名为与未修饰的类相同的名称来避免错误:

new_cls.__name__ = cls.__name__

然后代码运行没有错误:

import pickle

def class_decorator(cls):
class new_cls(cls):
def run(self, *args, **kwargs):
print 'In decorator'
super(new_cls,self).run(*args, **kwargs)
new_cls.__name__ = cls.__name__
return new_cls

@class_decorator
class cls(object):
def run(self):
print 'called'

a = cls()
print(a)
# <__main__.cls object at 0x7f57d3743650>

a.run()
# In decorator
# called

s = pickle.dumps(a)
# Note "cls" in the `repr(s)` below refers to the name of the class. This is
# what `pickle.loads` is using to unpickle the string
print(repr(s))
# 'ccopy_reg\n_reconstructor\np0\n(c__main__\ncls\np1\nc__builtin__\nobject\np2\nNtp3\nRp4\n.'

b = pickle.loads(s)
print(b)
# <__main__.cls object at 0x7f57d3743690>

b.run()
# In decorator
# called

关于python - 如何将类装饰器与pickle一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34253069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com