
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to serialize a Python class?
Object serialization and deserialization are a routine aspect of any non-trivial Python program. Saving to a file, reading a configuration file, responding to an HTTP request, all involve object serialization and deserialization.
Serialization and deserialization involve various schemes, formats and protocols to stream Python objects and to get them back later intact. Serialization schemes, formats or protocols you choose determine how fast your program runs and how secure it is.
Lets use a dictionary of python objects to serialize. Class is also a Python object. We use a Python module named pickle and its method pickle.dumps(object).
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.HIGHEST_PROTOCOL)
- Related Articles
- How to serialize Python dictionary to XML?
- How to serialize a JavaScript array?
- How to serialize a lambda function in Java?
- How to serialize a Polygon object using FabricJS?
- How do I serialize a Python dictionary into a string, and then back to a dictionary?
- Serialize and Deserialize BST in Python
- How to dynamically load a Python class?
- How can Keras be used to save and serialize the model using Python?
- How to serialize a Polyline object into JSON in FabricJS?
- How to use jQuery.serializeArray() method to serialize data?
- How to serialize and de-serialize generic types using the Gson library in Java?\n
- How to convert a string to a Python class object?
- How to serialize a JSON string to an Output Handler in Java?
- How to serialize a null field using Gson library in Java?
- How to de-serialize a Polyline object from JSON in FabricJS?

Advertisements