How do I serialize a Python class?

Python Methods and Functions

Object serialization and deserialization are a common aspect of any non-trivial Python program. Saving to file, reading configuration file, response to HTTP request &it all involves serializing and deserializing an object. 

Serialization and deserialization includes various schemes, formats, and protocols for streaming Python objects and then restoring them. The serialization schemes, formats or protocols you choose determine how fast your program runs and how secure it is.



Let's use a Python object dictionary for serialization. The class is also a Python object. We are using a Python module named pickle and its pickle.dumps (object) method.


 foo = dict (int_list = [3, 4, 5], text = 'Hello World', number = 9.99, boolean = False, none = None) import cPickle as pickle print pickle.dumps (foo) print pickle.dumps (foo, protocol = pickle.HIGHE ST_PROTOCOL) 







Get Solution for free from DataCamp guru