You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a class (lets call it MyClass) that wraps a pint.Quantity object and wants to provide some maths on it via sub etc.
Now I have something like
m=MyClass(...)
u=pint.Quantity(...)
x=m-u
Now what I would like to happen is that my classes sub handles this, however due to the python rules ( both are not the same type ) it first tries u.rsub(m) which will ultimately fail in quantity.py PlainQuantity._add_sub() because of the first checks there and a throw of DimensionalityError.
Now a common good practice is to handle the rsub case only for when you really know how to handle the other case, and otherwise return NotImplemented from within u.__rsub__(m) to then have python try m.__sub__(u) which would be the proper implementation in my case.
This might work if all the __r math functions would catch the DimensionalityError and return NotImplemented instead, because in most cases that error simply means that PlainQuantity got a type there it can't work with.
The text was updated successfully, but these errors were encountered:
I have a class (lets call it MyClass) that wraps a pint.Quantity object and wants to provide some maths on it via sub etc.
Now I have something like
Now what I would like to happen is that my classes sub handles this, however due to the python rules ( both are not the same type ) it first tries u.rsub(m) which will ultimately fail in quantity.py PlainQuantity._add_sub() because of the first checks there and a throw of DimensionalityError.
Now a common good practice is to handle the rsub case only for when you really know how to handle the other case, and otherwise
return NotImplemented
from withinu.__rsub__(m)
to then have python trym.__sub__(u)
which would be the proper implementation in my case.This might work if all the __r math functions would catch the DimensionalityError and return NotImplemented instead, because in most cases that error simply means that PlainQuantity got a type there it can't work with.
The text was updated successfully, but these errors were encountered: