-
-
Save TaiSakuma/a7d5d0fd343772e56385a8287093bc1c to your computer and use it in GitHub Desktop.
How to make it picklable?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import functools | |
import pickle | |
# https://stackoverflow.com/questions/20093811/how-do-i-change-the-representation-of-a-python-function/20094262#20094262 | |
class simple_repr(object): | |
def __init__(self, functor): | |
self.functor = functor | |
self.__name__ = functor.__name__ | |
self.__doc__ = functor.__doc__ | |
def __call__(self, *args, **kwargs): | |
return self.functor(*args, **kwargs) | |
def __repr__(self): | |
return self.functor.__name__ | |
@simple_repr | |
def func(a, b): | |
print(a, b) | |
print(str(func)) | |
print(repr(func)) | |
p = pickle.dumps(func) | |
hunc = pickle.loads(p) | |
print(str(hunc)) | |
print(repr(hunc)) | |
hunc(a='def', b=123) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment