What function do you call to retrieve and unpickle an object?
In Python, the function named pickle.load() is used to retrieve and unpickle an object.
The function named pickle.load() is present in the pickle module. It is used to deserialize and load a pickled object from a file. The pickle.load() function is designed to load objects that were previously serialized using pickle.dump() or other compatible serialization methods.
The following program helps to understand how to retrieve and unpickle an object:
import pickle
# Define a class
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
# Create an instance of the class
employee = Employee("John Doe", 3000)
# Serialize the object to a file
with open("employee.pickle", "wb") as file:
pickle.dump(employee, file)
# Deserialize the object from the file
with open("Employee.pickle", "rb") as file:
retrieved_employee = pickle.load(file)
# Print the retrieved object
print(retrieved_employee.name)
print(retrieved_employee.salary)
Output:
John Doe
3000
>>>
Output File: