I tried to overload the str and repr methods for a python function, like so:
def func():
pass
func.__str__ = lambda: 'func_str'
func.__repr__ = lambda: 'func_repr'
Calling these methods directly produces the exepcted output:
print(func.__str__())#output: func_str
print(func.__repr__())#output: func_repr
But calling the str() and repr() methods on the func object directly seems to ignore the overloaded methods:
print(str(func)) # <function func at 0x0000023C148CC310>
print(repr(func)) # <function func at 0x0000023C148CC310>
Question: Why is str(func)
not the same as func.__str__()
? If this is not the intended way of giving a function a str()
and repr()
, what would be the best approach?