python picklable

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要强大的多(但是不可读)

参考

https://stackoverflow.com/questions/3603581/what-does-it-mean-for-an-object-to-be-picklable-or-pickle-able

接下来将会整理whoosh相关的文档,学习whoosh的时候同时练习一下翻译,希望专栏申请能尽快通过!
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值