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

Discussion in 'Python' started by Petr Prikryl, Dec 13, 2004.

  1. Petr Prikryl

    Petr Prikryl Guest

    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)
    Petr Prikryl, Dec 13, 2004
    #1
    1. Advertising

  2. 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?
    >
    >
    Christian Ergh, Dec 13, 2004
    #2
    1. Advertising

  3. 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
    Steven Bethard, Dec 13, 2004
    #3
  4. 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.
    Istvan Albert, Dec 13, 2004
    #4
  5. On Mon, 13 Dec 2004 16:42:28 +0100, rumours say that "Petr Prikryl"
    <> 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...
    Christos TZOTZIOY Georgiou, Dec 13, 2004
    #5
  6. 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
    Pierre Barbier de Reuille, Dec 14, 2004
    #6
  7. Petr Prikryl

    Doug Holton Guest

    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
    Doug Holton, Dec 14, 2004
    #7
  8. Petr Prikryl

    Doug Holton Guest


    > 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.
    Doug Holton, Dec 21, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Balaji
    Replies:
    3
    Views:
    10,075
  2. Petr Prikryl
    Replies:
    4
    Views:
    328
    Mel Wilson
    Dec 14, 2004
  3. Terry Reedy
    Replies:
    2
    Views:
    261
    Nick Craig-Wood
    Dec 15, 2004
  4. Good Night Moon
    Replies:
    9
    Views:
    265
    Rick DeNatale
    Jul 25, 2007
  5. Mark Richards
    Replies:
    3
    Views:
    302
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page