New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Binding arithmetics-assign operators with non-standard C++ signature lead to surprising results #1793
Comments
It's kinda surprising, but kinda not, given that's how Python operators work? If you write the same code in Python 3, you'll get the same thing: class A:
def __iadd__(self, other):
pass
def __isub__(self, other):
return self
a1, a2 = A(), A()
a1 -= a2
print(a1)
# <__main__.A object at 0x7f34b275aa58>
a1 += a2
print(a1)
# None |
From Python docs (emphasis mine):
|
Thank you @eacousineau for looking this up! I agree that it make sense to assume that C++ operator should have correct signature. In case they does not: would it be too much trouble to add a fail safe and make sure that compilation fail if return type is unexpected? |
I think it is, because it is in the Python docs, would break consistency with Python (and might break some exotic cases where a different type is intentionally returned). Using |
(Thanks, @eacousineau for answering this!) |
Looks like bindings for
__iadd__
/__isub__
operators uses return value of C++ function to change value of current object. So if corresponding C++operator+=
returnvoid
(instead of standardT&
) - then value of object became PythonNone
. This behavior is quite surprising especially since corresponding C++ code will behave normally.Code to reproduce:
The text was updated successfully, but these errors were encountered: