Given a function foo
:
def foo(x):
pass
Printing its representation by invoking str
or repr
give
If you find yourself wrapping functions, it's useful to look at functools.partial
. It's primarily for binding arguments of course, but that's optional. It's also a class that wraps functions, removing the boilerplate of doing so from scratch.
from functools import partial
class foo(partial):
def __str__(self):
return "I'm foo!"
@foo
def foo():
pass
assert foo() is None
assert str(foo) == "I'm foo!"