Last active
January 30, 2016 01:24
-
-
Save lkilcher/d1e2193cbc366adb49c8 to your computer and use it in GitHub Desktop.
Bugs related to NumPy's new __numpy_ufunc__ hook.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
class buggy(object): | |
def __init__(self, arr): | |
self.arr = arr | |
def __pow__(self, other): | |
print "In __pow__" | |
return self.arr ** other | |
def __rpow__(self, other): | |
print "In __rpow__" | |
return other ** self.arr | |
def __ipow__(self, other): | |
print "In __ipow__" | |
self.arr **= other | |
return self | |
def __add__(self, other): | |
print "In __add__" | |
return self.arr + other | |
def __radd__(self, other): | |
print "In __radd__" | |
return other + self.arr | |
def __iadd__(self, other): | |
print "In __iadd__" | |
self.arr += other | |
return self | |
def __numpy_ufunc__(self, ufunc, method, i, inputs, **kwargs): | |
print "In __numpy_ufunc__" | |
a = np.arange(1, 6) | |
g = buggy(np.arange(2, 7)) | |
print 'DOING __iadd__' | |
g += a | |
print g.arr | |
a = np.arange(1, 6) | |
g = buggy(np.arange(2, 7)) | |
print 'DOING reversed __iadd__' | |
a += g | |
print a | |
a = np.arange(1, 6) | |
g = buggy(np.arange(2, 7)) | |
print 'DOING __ipow__' | |
g **= a | |
print g.arr | |
a = np.arange(1, 6) | |
g = buggy(np.arange(2, 7)) | |
print 'DOING reversed __ipow__' | |
a **= g |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I believe this is an unrelated sidenote and probably expected behavior. However, because it throws a similar error I'm pointing it out here.
I noticed that when the
__numpy_ufunc__
is not present in thebuggy
class, line 48 (a += g
) fails with:I suppose this is expected behavior because
__numpy_ufunc__
indicates to NumPy that it should be able to handle this type of object. That is, when__numpy_ufunc__
is not present it throws this error.