lundi 21 octobre 2019

Setting __hash__ of an existing numpy object

In my experiment, I have an instance of a numpy object whose __hash__ method I need to set.

I have tried various approaches (that seem to actually be the very same):

import numpy as np
x = np.array([1, 2, 3])

setattr(x, '__hash__', lambda self: 0)  # doesn't work without self either
AttributeError: 'numpy.ndarray' object attribute '__hash__' is read-only

x.__hash__ = lambda self: 0
AttributeError: 'numpy.ndarray' object attribute '__hash__' is read-only

Is there any way of forcing the object to accept my implementation? This is actually a broader question: is there a way of assigning any property/method to an existing object (including magic methods etc)?





Aucun commentaire:

Enregistrer un commentaire