auto-increment operator - why no syntax error?

  • Thread starter Karthik Gurusamy
  • Start date
K

Karthik Gurusamy

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)
2

Karthik
 
C

Chris M

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)


2

Karthik

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

Marc 'BlackJack' Rintsch

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)

2

There is no syntax error. It is just some unary pluses "chained". Maybe
unexpected but no syntax error.

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top