Is it possible to overwrite magic methods of JavaScript wrappers?

I am working with threejs and I would like to override some magic methods like __repr__ to make my debugging life a little easier.

For example, I have a Python v variable containing a THREE.Vector3 object. I have tried the following, hoping to get a more friendly representation, but it didn’t work:

print(v)
# -> <Vector3 proxyobject>
def new_repr(self):
    print(f'<{self.__class__.__name__}({self.x}, {self.y}, {self.z})>')
v.__repr__ = new_repr
v.__class__.__repr__ = new_repr
print(v)
# -> <Vector3 proxyobject>
repr(v)
# -> '<Vector3 proxyobject>'
new_repr(v)
# -> <Vector3(750, 1000, 600)>

It doesn’t work because I am trying the wrong way or because it is impossible?

Good question.

The object v and v.__class__ are both instance objects of the JavaScript Proxy type.

assert type(v) is type(v.__class__)

The .__class__ attribute is overridden to get you v’s JavaScript class.

In python setting a dunder on an instance has no effect on the underlying behaviour to be changed. There’s a section in the docs about it somewhere.

https://docs.python.org/3/reference/datamodel.html#special-method-lookup

I think the only way to do this is to wrap the object in a new class and create a custom repr.

class ThreeProxy:
    def __init__(self, v):
        self.v = v
    def __getattr__(self, attr):
        return getattr(self.v, attr)
    def __repr__(self):
        …