Skip to content

Instantly share code, notes, and snippets.

@TaiSakuma

TaiSakuma/e.py Secret

Last active May 12, 2019 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TaiSakuma/a7d5d0fd343772e56385a8287093bc1c to your computer and use it in GitHub Desktop.
Save TaiSakuma/a7d5d0fd343772e56385a8287093bc1c to your computer and use it in GitHub Desktop.
How to make it picklable?
#!/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