auto-increment operator - why no syntax error?

Chris M chris.monsanto at gmail.com
Sat Dec 8 20:11:08 EST 2007


On Dec 8, 7:58 pm, Karthik Gurusamy <kar1... at gmail.com> wrote:
> I see python doesn't have ++ or -- operators unlike say, C.
> I read some reasonings talking about immutable scalars and using ++/--
> doesn't make much sense in python (not sure if ++i is that far-fetched
> compared to the allowed i += 1)
>
> In any case, I accidentally wrote ++n in python and it silently
> accepted the expression and  it took me a while to debug the problem.
>
> Why are the following accepted even without a warning about syntax
> error?
> (I would expect the python grammar should catch these kind of syntax
> errors)
>
> >>> n = 1
> >>> 2 * +++++ n
> 2
> >>> n += 1
> >>> n
> 2
> >>> ++n
>
> 2
>
> Karthik

There is a unary operator "+". When you write ++n, it's evaluating +n,
and then +(that result).



More information about the Python-list mailing list