Skip to content

Instantly share code, notes, and snippets.

@TaiSakuma

TaiSakuma/e.py Secret

Last active May 12, 2019
Embed
What would you like to do?
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