I want to be able to repr()
on a class in python which has functions stored in the class's variables so I can reproduce the same functions later when I eval()
said output.
Here's an example to illustrate my problem:
class Example:
def __init__(self, func):
self.func = func
def __repr__(self):
return repr(self.func)
def add(x, y):
return x + y
example = Example(add)
print(repr(example))
When I run that code, the output I get is:
<function add at 0x7f6ea0e96e18>
Is there any way of making a repr()
which would output something that can then be eval()
ed to a callable function. Ideally, I'd like an output like:
def add(x, y): return x + y