
July 18th, 2005, 07:41 PM
| | | Suggestion for "syntax error": ++i, --i
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, ...))[color=blue][color=green][color=darkred]
>>> i = 1
>>> i[/color][/color][/color]
1[color=blue][color=green][color=darkred]
>>> i++[/color][/color][/color]
File "<stdin>", line 1
i++
^
SyntaxError: invalid syntax[color=blue][color=green][color=darkred]
>>> ++i[/color][/color][/color]
1[color=blue][color=green][color=darkred]
>>> --i[/color][/color][/color]
1[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
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) |

July 18th, 2005, 07:42 PM
| | | Re: Suggestion for "syntax error": ++i, --i
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.
[color=blue][color=green][color=darkred]
>>> i = 1
>>> i +=1
>>> i[/color][/color][/color]
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:[color=blue]
> 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, ...))
>[color=green][color=darkred]
>>>>i = 1
>>>>i[/color][/color]
>
> 1
>[color=green][color=darkred]
>>>>i++[/color][/color]
>
> File "<stdin>", line 1
> i++
> ^
> SyntaxError: invalid syntax
>[color=green][color=darkred]
>>>>++i[/color][/color]
>
> 1
>[color=green][color=darkred]
>>>>--i[/color][/color]
>
> 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?
>
>[/color] | 
July 18th, 2005, 07:42 PM
| | | Re: Suggestion for "syntax error": ++i, --i
Petr Prikryl wrote:[color=blue]
> Summary: In my opinion, the C-like prefix
> increment and decrement operators (++i and --i)
> should be marked as "syntax error".[/color]
This would give some weird assymetry:
[color=blue][color=green][color=darkred]
>>> i = 1
>>> ++i[/color][/color][/color]
Traceback ( File "<interactive input>", line 1
++i
^
SyntaxError: invalid syntax[color=blue][color=green][color=darkred]
>>> --i[/color][/color][/color]
Traceback ( File "<interactive input>", line 1
--i
^
SyntaxError: invalid syntax[color=blue][color=green][color=darkred]
>>> +-i[/color][/color][/color]
-1[color=blue][color=green][color=darkred]
>>> -+i[/color][/color][/color]
-1[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
Of course, anyone who writes +-i or -+i should probably be shot anyway.
;) I'm -0 on this.
Steve | 
July 18th, 2005, 07:42 PM
| | | Re: Suggestion for "syntax error": ++i, --i
Petr Prikryl wrote:
[color=blue]
> Summary: In my opinion, the C-like prefix
> increment and decrement operators (++i and --i)
> should be marked as "syntax error".[/color]
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. | 
July 18th, 2005, 07:42 PM
| | | Re: Suggestion for "syntax error": ++i, --i
On Mon, 13 Dec 2004 16:42:28 +0100, rumours say that "Petr Prikryl"
<Prikryl@skil.cz> might have written:
[color=blue]
>Summary: In my opinion, the C-like prefix
>increment and decrement operators (++i and --i)
>should be marked as "syntax error".[/color]
[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... | 
July 18th, 2005, 07:45 PM
| | | Re: Suggestion for "syntax error": ++i, --i
Christian Ergh a écrit :[color=blue]
> 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.
>[color=green][color=darkred]
> >>> i = 1
> >>> i +=1
> >>> i[/color][/color]
> 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
>[/color]
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 | 
July 18th, 2005, 07:46 PM
| | | Re: Suggestion for "syntax error": ++i, --i
Petr Prikryl wrote:[color=blue]
> Hi,
>
> Summary: In my opinion, the C-like prefix
> increment and decrement operators (++i and --i)
> should be marked as "syntax error".[/color]
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 | 
July 18th, 2005, 08:09 PM
| | | Re: Suggestion for "syntax error": ++i, --i
[color=blue]
> Petr Prikryl wrote:
>[color=green]
>> Hi,
>>
>> Summary: In my opinion, the C-like prefix
>> increment and decrement operators (++i and --i)
>> should be marked as "syntax error".[/color][/color]
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. | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 205,414 network members.
|