Q1. What is serialization in Python? A1. Serialization is the process of converting a Python object into a byte stream to store it in a file or transmit it over a network.
Q2. What is deserialization? A2. Deserialization is the process of converting a byte stream back into a Python object.
Q3. How can you serialize an object in Python? A3. You can serialize an object in Python by using the pickle module or the json module.
Q4. What is the pickle module used for? A4. The pickle module is used for object serialization in Python. It can convert Python objects into a byte stream and vice versa.
Q5. What is the difference between the pickle and json modules? A5. The pickle module is Python-specific and can serialize almost any Python object, while the json module is a standard library module that serializes/deserializes objects in a human-readable format and is limited to basic data types.
Q6. How do you serialize an object using the pickle module? A6. You can serialize an object using the pickle.dump() function.
Q7. How do you deserialize an object using the pickle module? A7. You can deserialize an object using the pickle.load() function.
Q8. What are some common use cases for serialization? A8. Some common use cases for serialization include storing object data in a file, sending objects over a network, and caching objects.
Q9. What are the potential security risks associated with deserialization? A9. Deserialization of untrusted data can lead to security vulnerabilities such as code execution attacks. It's important to validate and sanitize the deserialized data.
Q10. How can you handle serialization of custom objects in Python? A10. To handle serialization of custom objects, you can implement the getstate() and setstate() methods in your object class when using the pickle module.
Q11. Can you serialize Python objects that contain file handles or network connections? A11. No, you cannot directly serialize Python objects that contain file handles or network connections. You need to handle these situations separately.
Q12. What is the purpose of the json module in Python? A12. The json module in Python provides functions for serializing and deserializing objects to and from JSON format.
Q13. What data types can be serialized using the json module? A13. The json module can serialize basic data types such as strings, numbers, lists, dictionaries, booleans, and null.
Q14. How do you serialize an object using the json module? A14. You can serialize an object using the json.dumps() function.
Q15. How do you deserialize an object using the json module? A15. You can deserialize an object using the json.loads() function.
Q16. What is the difference between JSON and pickle serialization? A16. JSON serialization produces human-readable text, while pickle serialization produces a binary representation of the object.
Q17. Can you customize the serialization process with the json module? A17. Yes, you can customize the serialization process by defining a custom encoder class that inherits from json.JSONEncoder and override the default() method.
Q18. What are some alternatives to pickle and json for serialization in Python? A18. Some alternatives to pickle and json include the msgpack module, the yaml module, and the protobuf library.