How do I add magic method to an existing object using partial? (I understand that adding methods to an existing object is not a recommended concept.)
Here's what I've tried
from functools import partial
class Foo:
def __init__(self):
self.value = 10
def my_function(obj):
return str(obj.value)
obj = Foo()
obj.__repr__ = partial(my_function, obj)
print(repr(obj)) # <__main__.A object at 0x7ff612e62bb0>
print(obj.__repr__()) # 10
I was expecting print(repr(obj))
also prints 10, but it just returned memory address.