Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Suggestion for "syntax error": ++i, --i

Reply
Thread Tools

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

 
 
Petr Prikryl
Guest
Posts: n/a
 
      12-13-2004
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)
 
Reply With Quote
 
 
 
 
Christian Ergh
Guest
Posts: n/a
 
      12-13-2004
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?
>
>

 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      12-13-2004
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
 
Reply With Quote
 
Istvan Albert
Guest
Posts: n/a
 
      12-13-2004
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.


 
Reply With Quote
 
Christos TZOTZIOY Georgiou
Guest
Posts: n/a
 
      12-13-2004
On Mon, 13 Dec 2004 16:42:28 +0100, rumours say that "Petr Prikryl"
<(E-Mail Removed)> 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 RFC195
I really should keep that in mind when talking with people, actually...
 
Reply With Quote
 
Pierre Barbier de Reuille
Guest
Posts: n/a
 
      12-14-2004
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
 
Reply With Quote
 
Doug Holton
Guest
Posts: n/a
 
      12-14-2004
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
 
Reply With Quote
 
Doug Holton
Guest
Posts: n/a
 
      12-21-2004

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wireless Access Point Suggestion =?Utf-8?B?UyBMYW5l?= Wireless Networking 2 06-24-2005 10:39 PM
Suggestion for a new name, logo and slogan for the Mozilla Suite... Charles Firefox 2 03-12-2005 04:31 AM
"Read-only" version of Mozilla? (suggestion for a new software idea) Hallvard Tangeraas Firefox 4 01-12-2005 07:24 PM
FireFox-opening new tabs, a suggestion me Firefox 3 09-21-2004 04:19 PM
Re: Suggestion for Cache flush in Moz Caffeine Junkie Firefox 1 01-20-2004 06:06 AM



Advertisments