By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,569 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,569 IT Pros & Developers. It's quick & easy.

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

P: n/a
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, ...))
i = 1
i 1 i++ File "<stdin>", line 1
i++
^
SyntaxError: invalid syntax ++i 1 --i 1


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?
--
Petr Prikryl (prikrylp at skil dot cz)
Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.
i = 1
i +=1
i
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


Petr Prikryl wrote:
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, ...))
i = 1
i
1
i++
File "<stdin>", line 1
i++
^
SyntaxError: invalid syntax
++i
1
--i


1
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?

Jul 18 '05 #2

P: n/a
Petr Prikryl wrote:
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:
i = 1
++i Traceback ( File "<interactive input>", line 1
++i
^
SyntaxError: invalid syntax --i Traceback ( File "<interactive input>", line 1
--i
^
SyntaxError: invalid syntax +-i -1 -+i -1


Of course, anyone who writes +-i or -+i should probably be shot anyway.
;) I'm -0 on this.

Steve
Jul 18 '05 #3

P: n/a
Petr Prikryl wrote:
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.
Jul 18 '05 #4

P: n/a
On Mon, 13 Dec 2004 16:42:28 +0100, rumours say that "Petr Prikryl"
<Pr*****@skil.cz> might have written:
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
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 18 '05 #5

P: n/a
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.
>>> i = 1
>>> i +=1
>>> i

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
Jul 18 '05 #6

P: n/a
Petr Prikryl wrote:
Hi,

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


We have a patch for increment and decrement operators in boo (
http://boo.codehaus.org/ ), along with an operator overloading syntax
like below. See http://jira.codehaus.org/browse/BOO-223

def +:
pass
Jul 18 '05 #7

P: n/a
Petr Prikryl wrote:
Hi,

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

Let me rephrase my answer.
This is a good sugestion for Python 3.0, a.k.a. Python 3000:
http://www.python.org/cgi-bin/moinmoin/Python3.0

In the future you may be able to to this:

variable++

However, Python 3.0 is likely years away. If you want to know how to
run this code today, ask Fredrik Lundh.
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.