i want to ask why we should pickle an object in python ?
why the file that we want to pickle should be opened in binary mode ?
and what is the HIGHEST_PROTOCOL used in pickling ?
Asked
Active
Viewed 163 times
0

CDspace
- 2,639
- 18
- 30
- 36

D.AhmedRafik
- 81
- 2
- 15
-
2The [docs](https://docs.python.org/2/library/pickle.html#pickle.HIGHEST_PROTOCOL) might be a good place to start – CDspace Jun 04 '14 at 22:42
-
You can look at [JSON](https://docs.python.org/2/library/json.html) as well. – dawg Jun 04 '14 at 22:44
-
didn't understood it too much :/ – D.AhmedRafik Jun 04 '14 at 22:44
-
As far as why pickle, this might be of some help http://stackoverflow.com/questions/3438675/common-use-cases-for-pickle-in-python?rq=1 – CDspace Jun 04 '14 at 22:47
-
kind of a duplicate… see here: http://stackoverflow.com/questions/8968884/python-serialization-why-pickle/19360828#19360828 – Mike McKerns Jun 04 '14 at 23:05
1 Answers
4
- You don't "have to" pickle an object, but in case you want to (for purpose of saving it, sending it etc) you can use pickling (serializing).
- from the docs:
Note: Be sure to always open pickle files created with protocols >= 1 in binary mode. ...
it means that only files that weren't created using ASCII protocol (protocol 0) should be opened in binary mode. As for why - the answer is probably in the implementation of pickling.
HIGHEST_PROTOCOL
is another way of naming the "latest" protocol

Nir Alfasi
- 53,191
- 11
- 86
- 129