Skip to content

Instantly share code, notes, and snippets.

@milesrout
Forked from louisswarren/named_func.py
Last active June 6, 2016 00:05
Show Gist options
  • Save milesrout/a3b362a155103dcfa8c381373fc2df70 to your computer and use it in GitHub Desktop.
Save milesrout/a3b362a155103dcfa8c381373fc2df70 to your computer and use it in GitHub Desktop.
Functions with human-readable representations
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