Hi,

I think changing Add.__add__ to return NotImplemented is the right
solution. This is the Python behavior for what you want. Python will raise
an error if Add.__add__ return NotImplemented and y.__radd__(x) don't work.

There isn't any other way to do this to my knowledge.

Frédéric


On Wed, Jul 3, 2013 at 10:47 AM, Prasoon Shukla <[email protected]>wrote:

> In Python, we have special methods that when used in a class definition,
> overload standard operators.
>
> Now, here's the problem I am having (shown as an example):
>
> Let us say we have a vector - which is an instance of a class that has
> been subclassed from Basic. Let's call this class Vector. Then, I have a
> container class, VectMul (subclassed from Mul), that is used to hold any
> objects of type *scalar * vector - *much like Mul does. I won't go too
> deep into what else VectMul class does.
>
> Anyway, now, I have defined a method Vector.__mul__ which is supposed to
> return a VectMul after performing appropriate checks on the input.
>
> At this point, let me just refresh your memory : if x and y are python
> objects, then:
> x + y => x.__add__(y)
> y + x => y.__add__(x)
>
> Back to my question. So, when a user does:
>
>
>    - Vector*Vector, the Vector.__mul__ method will raise an error saying
>    that a user cannot multiply two vectors.
>    - Vector*scalar, the Vector.__mul__ will be called and a VectMul will
>    be returned.
>    - *scalar*Vector : *Now this is the problem - here let us say that
>    scalar is a Add - then the method Add.__mul__ will be called instead of
>    what I want to be called  - in this case, Vector.__mul__ - and return a Mul
>    object instead of a VectMul.
>
> This is the problem I face right now. I googled around a bit - apparently,
> we can use the __rmul__ method. But, for that to work - the Add.__mul__ (in
> the third point above) should return NotImplemented (what does that even
> mean? Raise an exception?). Obviously, I shouldn't have to change
> Add.__add__ for this. So, how can I handle this problem?
>
>  --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sympy.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to