python中pickle模块无法导入_Python:无法pickle模块对象

根据文件:What can be pickled and unpickled?

The following types can be pickled:None, True, and False

integers, floating point numbers, complex numbers

strings, bytes, bytearrays

tuples, lists, sets, and dictionaries containing only picklable objects

functions defined at the top level of a module (using def, not lambda)

built-in functions defined at the top level of a module

classes that are defined at the top level of a module

instances of such classes whose __dict__ or the result of calling __getstate__() is picklable (see section Pickling Class Instances for details).

如您所见,模块不在列表中。注意,这在使用deepcopy时也是正确的,而且不仅适用于pickle模块,如deepcopy文档中所述:This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types. It does “copy” functions and classes (shallow and deeply), by returning the original object unchanged; this is compatible with the way these are treated by the pickle module.

可能的解决方法是使用@property装饰器而不是属性。

例如,这应该有效:import numpy as np

import pickle

class Foo():

@property

def module():

return np

foo = Foo()

with open('test.out', 'wb') as f:

pickle.dump(foo, f)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值