-
-
Save milesrout/a3b362a155103dcfa8c381373fc2df70 to your computer and use it in GitHub Desktop.
Functions with human-readable representations
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
import inspect | |
def named_func(f): | |
return type('named_func', (), { | |
'__call__': lambda s, *a, **k: f(*a, **k), | |
'__repr__': lambda s: inspect.getsource(f), | |
'__str__': lambda s: f.__name__, | |
'__name__': f.__name__, | |
'__doc__': f.__doc__, | |
})() | |
>>> @named_func | |
... def foo(x): | |
... return x*x | |
... | |
>>> print(repr(foo)) | |
@named_func | |
def foo(x): | |
return x*x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment