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

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

  1. Petr Prikryl

    Petr Prikryl Guest

    Hi Christian,

    The suggestion is to mark PREFIX version ++i as
    syntax error. It is not related to the postfix
    version of the ++ operator. For prefix in/decrement,
    there is no extra variable behind. But again,
    it is not related to the suggestion. The postfix
    version is already marked as "syntax error".
    The suggestion is to do the same with the prefix
    version of the operators that are not used in Python.

    Petr

    "Christian Ergh" wrote...
    > 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?
    > >
    > >
    Petr Prikryl, Dec 13, 2004
    #1
    1. Advertising

  2. Ah, ok, i misunderstood you. Well, to mark it as a syntax error sounds
    good, and at the Moment I would not know a case where this conflicts
    with a implementation.
    Chris
    Petr Prikryl wrote:
    > Hi Christian,
    >
    > The suggestion is to mark PREFIX version ++i as
    > syntax error. It is not related to the postfix
    > version of the ++ operator. For prefix in/decrement,
    > there is no extra variable behind. But again,
    > it is not related to the suggestion. The postfix
    > version is already marked as "syntax error".
    > The suggestion is to do the same with the prefix
    > version of the operators that are not used in Python.
    >
    > Petr
    >
    > "Christian Ergh" wrote...
    >
    >>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

    Peter Maas Guest

    Christian Ergh schrieb:
    > Ah, ok, i misunderstood you. Well, to mark it as a syntax error sounds
    > good, and at the Moment I would not know a case where this conflicts
    > with a implementation.


    target = 'a='
    sign = '-'
    operand = '-2'

    exec(target+sign+operand)

    --
    -------------------------------------------------------------------
    Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
    E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
    -------------------------------------------------------------------
    Peter Maas, Dec 13, 2004
    #3
  4. Peter Maas wrote:
    >
    > target = 'a='
    > sign = '-'
    > operand = '-2'
    >
    > exec(target+sign+operand)


    Hopefully not too many people write code like above when it isn't much
    harder to write this without exec:

    >>> target = 'a'
    >>> sign = '-'
    >>> operand = '-2'
    >>> sign_functions = {'+':eek:perator.pos, '-':eek:perator.neg}
    >>> globals()[target] = sign_functions[sign](int(operand))
    >>> a

    2

    This avoids fun with exec like:

    target = 'a='
    sign = '-'
    operand = '-2; import os; os.removedirs("/")'

    =)

    Steve
    Steven Bethard, Dec 13, 2004
    #4
  5. Petr Prikryl

    Mel Wilson Guest

    In article <cpkgda$pt3$04$-online.com>,
    Christian Ergh <> wrote:
    >Ah, ok, i misunderstood you. Well, to mark it as a syntax error sounds
    >good, and at the Moment I would not know a case where this conflicts
    >with a implementation.


    Well, you can overload prefix `+` and `-` operators on an
    object by defining __pos__ and __neg__ methods, so --a, ++a,
    +-a, -+a, +-+a and other similar combinations are actually
    good syntax, and as useful as you want them to be.

    Regards. Mel.
    Mel Wilson, Dec 14, 2004
    #5
    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,074
  2. Petr Prikryl

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

    Petr Prikryl, Dec 13, 2004, in forum: Python
    Replies:
    7
    Views:
    356
    Doug Holton
    Dec 21, 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