Python allows an arbitrary number of repeated - and +.  The
implementation calls __neg__ or __pos__ for each but the leftmost
operand, which calls __add__ or __sub__:

In [14]: class MyClass(object):
   ....:     def __neg__(self):
   ....:         print "neg self"
   ....:         return self
   ....:     def __sub__(self, other):
   ....:         print "sub"
   ....:         return self
   ....:     def __pos__(self):
   ....:         print "pos self"
   ....:         return self
   ....:     def __add__(self, other):
   ....:         print "add"
   ....:         return self
   ....:

In [15]: a = MyClass()

In [16]: b = MyClass()

In [17]: a +-+-+++---+-- b
neg self
neg self
pos self
neg self
neg self
neg self
pos self
pos self
pos self
neg self
pos self
neg self
add
Out[17]: <__main__.MyClass object at 0x105de7ad0>

In [18]: a --++-+-+++-+ b
pos self
neg self
pos self
pos self
pos self
neg self
pos self
neg self
pos self
pos self
neg self
sub
Out[18]: <__main__.MyClass object at 0x105de7ad0>

This is pretty much what you'd expect.  Multiple + and - cancel each other out.

Aaron Meurer

On Wed, Aug 10, 2011 at 2:04 AM, Chris Smith <[email protected]> wrote:
> On Wed, Aug 10, 2011 at 1:09 PM, Zolzaya Erdenebaatar
> <[email protected]> wrote:
>> Hi guys, I'm new to sympy. I'm trying to calculate expression using
>> sympy in python. When I trying to below expression I get a result. But
>> expression is syntax is invalid /double subtract before 8/.
>>
>> from sympy import S
>> exp = S('(25+8)*2/6+9/3--8')
>> print exp.evalf()
>>
>> Why? Is there any way to validate expression?
>
>
> I get 22 which is correct. A double (or even triple) minus is allowed
> in python syntax:
>
>    >>> 3 --- 8 # 3 neg
>    -5
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/sympy?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to