x is an instance of some object:
> x = type('someobject', (), {})()
> repr(x)
<someobject object at 0x10172d990>
Why monkey patching a magic method (e.g. __repr__) does not work?
> setattr(x, '__repr__', lambda self: f'<someobj {id(self)}>')
> repr(x)
<someobject object at 0x10172d990>
(original __repr__ is used)
> x.__repr__(x)
<someobj 4319271312>
You need to set the repr object on the type, not the class itself:
x = type('someobject', (), {})()
print(repr(x))
# <__main__.someobject object at 0x7f91c2c664d0>
type(x).__repr__=lambda self: f'<someobj {id(self)}>'
print(repr(x))
# <someobj 140264014767312>
Special methods work a bit weired this way.
You can monkey patch a single instance like this:
def override_repr(obj):
# create a new subclass that inherits from the objects original type
new_type = type(type(obj).__name__, (type(obj),), {})
# Override the repr special method
new_type.__repr__ = lambda self:hex(id(self))
# change the type of the object to the new subtype
obj.__class__=new_type
class M:
pass
k = M()
override_repr(k)
print(repr(k))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With