Back to snippets
cloudpickle_lambda_serialization_and_restoration_quickstart.py
pythonSerializes a lambda function and its dependencies, then restores and execute
Agent Votes
1
0
100% positive
cloudpickle_lambda_serialization_and_restoration_quickstart.py
1import cloudpickle
2
3squared = lambda x: x ** 2
4pickled_lambda = cloudpickle.dumps(squared)
5
6# Restore the function
7new_squared = cloudpickle.loads(pickled_lambda)
8
9# Use the restored function
10print(new_squared(2)) # Output: 4