Suggestion for "syntax error": ++i, --i

P

Petr Prikryl

Hi,

Summary: In my opinion, the C-like prefix
increment and decrement operators (++i and --i)
should be marked as "syntax error".


Current situation: try... (Python 2.4 (#60, ...)) File "<stdin>", line 1
i++
^
SyntaxError: invalid syntax
Reason for how ++i and --i behaves in Python is
that it is probably treated as (-(-i)) and (+(+i))
respectively.

Rationale: My guess is that many Python users
do use other languages at the same time.
The C-family languages do use the prefix increment
and decrement operators. When used in Python
no warning appears -- the code simply does not work
as expected. In the same time, there is probably no
reason to use the increment and decrement
prefix operators. On the other hand, the newcommer
or the programmer that "types ++i automatically
because of brain synapses say he/she should..."
is not warned until something does not work.

Con: The situation must be recognized by the parser
(i.e. someone have to implement it).

Pro: No runtime overhead except of the detection
of the situation. As Python is a good candidate
to be used as the language for teaching, the "syntax
error" would be the pedagogical plus.

Personal experience: Many things in Python work
intuitively. My C++ experience "forced me" to use
++i as described above. I use iteration more these
days and I know about the ++i problem invisibility
in Python. But I had to learn by mistake. The ++i
behaviour is not intuitive for me.

Your opinion?
 
C

Christian Ergh

Hmm, i never liked the i++ syntax, because there is a value asignment
behind it and it does not show - except the case you are already used to it.
2

I like this one better, because you see the assignment at once, it is
easy to read and inuitive usability is given - in my opinion.
Chris
 
S

Steven Bethard

Petr said:
Summary: In my opinion, the C-like prefix
increment and decrement operators (++i and --i)
should be marked as "syntax error".

This would give some weird assymetry:
Traceback ( File "<interactive input>", line 1
++i
^
SyntaxError: invalid syntaxTraceback ( File "<interactive input>", line 1
--i
^
SyntaxError: invalid syntax
Of course, anyone who writes +-i or -+i should probably be shot anyway.
;) I'm -0 on this.

Steve
 
I

Istvan Albert

Petr said:
Summary: In my opinion, the C-like prefix
increment and decrement operators (++i and --i)
should be marked as "syntax error".

My guess is that the impact of it would be nil.

This is python, there are no prefix or postfix
operators. That is very easy to remember. Just because
one might get burned by it when learning python
it cannot become a recurring problem that needs fixing.

Istvan.
 
C

Christos TZOTZIOY Georgiou

Summary: In my opinion, the C-like prefix
increment and decrement operators (++i and --i)
should be marked as "syntax error".

[snip of lots of explanations]

I am +0 on this.

However, I can imagine no *non-obscure* reasons[1] for someone to use ++
and -- as prefixes or postfixes[2], so I think that it's possible to
modify the lexer or grammar (ain't sure) to make ++ and -- without any
spaces in-between be a valid operator somehow throwing a SyntaxError.

I have been bitten by this in the beginning[3], but OTOH it didn't last
long. I am on the plus side of 0 just because of a couple of lines of
the python Zen:

Errors should never pass silently. (although it's only a logical error)
In the face of ambiguity, refuse the temptation to guess.

my 2e-2 euros



[1] including overloaded operators and side-effects
[2] "p++ - x" is valid python even if "p++" is not
[3] at least Javascript and [ng]awk accept these operators
 
P

Pierre Barbier de Reuille

Christian Ergh a écrit :
Hmm, i never liked the i++ syntax, because there is a value asignment
behind it and it does not show - except the case you are already used to
it.

2

I like this one better, because you see the assignment at once, it is
easy to read and inuitive usability is given - in my opinion.
Chris

IMO, there is an assignement only for languages like python ! (ie.
reference languages)

In languages like C or C++ (in which variables correspond to values) it
makes perfect sens to modify the current value by an operator.

Nevertheless, I agree with you concerning Python ! But not for C++ :)

Pierre
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top