[Python-Dev] surprised to "++" and "--"

Gary Herron gherron at islandtraining.com
Fri Sep 25 11:09:11 EDT 2009


(Moving this to python-list where it should have been in the first place).)

As pointed out, var++ and var-- are syntax errors, however several 
similar expressions are legal.  These, however, are not interpreted as 
the C++ increment and decrement operators, but rather as a binary 
operator followed by a unary operator or two unary operators.

++a, --a, a++b, a--b are all legal and interpreted as +(+a), -(-a), 
a+(+b), and a-(-b). 

Could it be from one of these that the OP formed his (mistaken) idea?

Gary Herron



Facundo Batista wrote:
> 2009/9/23 Wee Binn <weebinn at gmail.com>:
>
>   
>>     I know that there is no "++" or "--" operator in python, but if "var++"
>> or something like that in my code(you know, most of C/C++ coders may like
>> this),there is nothing wrong reported and program goes on just like
>> expected!!
>>     This is obscure, maybe a bug.
>>     
>
> You can not write "var++":
>
>   
>>>> var = 5
>>>> var++
>>>>         
>    File "<ipython console>", line 1
>      var++
>           ^
> SyntaxError: invalid syntax
>   
>
>   

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090925/31a80f05/attachment.html>


More information about the Python-list mailing list