What does it mean for an object to be picklable(or pickle-able)?
我第一次遇到这个问题是在看whoosh文档的时候。
Indexed text fields must be passed a unicode value. Fields that are stored but not indexed (STORED field type) can be passed any pickle-able object.
can be passed any pickle-able object
字段被存储但是没有被索引(STORED字段类型)的任何pickle-able对象都可以使用。
这个问题是在stack Overflow上找到答案的。
因为谷歌翻译也没有找到.
answer:
It simply means it can be serialized by the pickle module. For a basic explanation of this, see What can be pickled and unpickled?
The pickle protocol provides more details, and shows how classes can customize the process.
简单理解,就是可以被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).
None, True, False
int, float, 复数
str, bytes, 字节数组
元祖,列表,集合,字典里面都是可以被pickle的对象
最外层定义的函数(使用def,不是lambda)
模块里python自带的最外层定义的内建函数。
模块里面最外层定义的类
类里面__dict__和__getstate__()可以被序列化的类的实例。
another:
Things that are usually not pickable are, for example, sockets, file(handler)s, database connections, and so on. Everything that’s build up (recursively) from basic python types (dicts, lists, primitives, objects, object references, even circular) can be pickled by default.
You can implement custom pickling code that will, for example, store the configuration of a database connection and restore it afterwards, but you will need special, custom logic for this.
All of this makes pickling a lot more powerful than xml, json and yaml (but definitely not as readable)
python一切皆对象,通常情况下不是所有的对象都是被序列化的。
举个例子,sockets对象,文件,数据库链接等。
任何从python来的内建类型都可以被序列化。
你可以实现自定义的序列化代码,举个例子,存储数据库配置的连接并且在之后重连,你需要特殊的自定义逻辑来实现序列化。
所有的这些使得序列化比xml,json,yaml要强大的多(但是不可读)