You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The objects created as instantiations of the class LiquidMetalInterface are not pickable. To reproduce this limitation, try this code:
import pickle
from lbh15 import Lead
ll=Lead(T=1000)
with open('test.dat', 'wb') as file:
pickle.dump(ll, file, protocol=pickle.DEFAULT_PROTOCOL)
The following error is returned: AttributeError: Can't pickle local object 'LiquidMetalInterface.__add_property.<locals>.<lambda>'
An extension should be implemented to make such objects pickable, allowing them to be serialized and saved into a file.
The reason why a LiquidMetalInterface object is not pickable is because a lambda function is used within the __add_property() method of _lbh15.py module. Lambda functions create anonymous function objects, while pickle records references to functions as module-level names, and not the functions themselves. So, an alternative solution should be sought.
In issue #63 , the Lambda function was introduced to simplify the implementation by avoiding the definition of the inner function new_property_info(). However, the previous solution also doesn't make an instance of the LiquidMetalInterface class pickable. The reason for this is that the inner functions do not make their names available at the module-level either.
So, among the others, two options could be suggested:
to define an external module-level function, that should be passed as argument to the function setattr() in the method __add_property();
The solution implemented in commit 5703f93 is to replace the lambda function passed to setattr() in the __add_property() method with the private __property_info() method of the LiquidMetalInterface class. This way pickle can reference the name of this private method. This also implies the need to use functools.partial to define the method to add to the LiquidMetalInterface instance in __add_property(), so that you can only define the method to call and not call it.
The objects created as instantiations of the class
LiquidMetalInterface
are not pickable. To reproduce this limitation, try this code:The following error is returned:
AttributeError: Can't pickle local object 'LiquidMetalInterface.__add_property.<locals>.<lambda>'
An extension should be implemented to make such objects pickable, allowing them to be serialized and saved into a file.
The reason why a
LiquidMetalInterface
object is not pickable is because alambda
function is used within the__add_property()
method of_lbh15.py
module.Lambda
functions create anonymous function objects, whilepickle
records references to functions as module-level names, and not the functions themselves. So, an alternative solution should be sought.In issue #63 , the
Lambda
function was introduced to simplify the implementation by avoiding the definition of the inner functionnew_property_info()
. However, the previous solution also doesn't make an instance of theLiquidMetalInterface
class pickable. The reason for this is that the inner functions do not make their names available at the module-level either.So, among the others, two options could be suggested:
setattr()
in the method__add_property()
;functools.partial
according to what stated, for instance, in https://docs.python.org/3/library/functools.html#partial-objects and https://stackoverflow.com/questions/58361373/are-partial-functions-officially-picklable.The text was updated successfully, but these errors were encountered: