Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

Consider not using lambdas as default arguments to enable pickling #41

Closed
benjamin-work opened this issue May 14, 2018 · 4 comments
Closed
Assignees

Comments

@benjamin-work
Copy link
Contributor

Problem description

Some classes have lambdas as default arguments (e.g. here). This prevents these objects from being pickled.

Steps to Reproduce the Problem

import pickle
from torchnlp.text_encoders import StaticTokenizerEncoder

encoder = StaticTokenizerEncoder(['hi', 'you'])
pickle.dumps(encoder) 

# raises error
PicklingError: Can't pickle <function StaticTokenizerEncoder.<lambda> at 0x7fb77c042b70>: attribute lookup StaticTokenizerEncoder.<lambda> on torchnlp.text_encoders.static_tokenizer_encoder failed

Solution

Replace lambdas by actual functions.

@PetrochukM PetrochukM self-assigned this May 15, 2018
@PetrochukM
Copy link
Owner

Until I get a freecycle to get to this, consider using dill a substitute for pickle that supports lambda functions.

@benjamin-work
Copy link
Contributor Author

Yes, dill would work, but some libs resort to pickle (joblib). Maybe when I have time, I could make a PR for this but I wanted to make sure that this is something you would like to change.

@PetrochukM
Copy link
Owner

Ahh, Gotcha. Thanks for your patience. Feel free to make a PR this. This sounds like a super reasonable use case.

@PetrochukM
Copy link
Owner

Thanks for your PR! Merged it with a couple updates.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants