python, why is str(function) ignoring function.__str__()?

I tried to overload the str and repr methods for a python function, like so:

def func():

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?

Read more here:

Content Attribution

This content was originally published by LcdDrm at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: