auto-increment operator - why no syntax error?

Discussion in 'Python' started by Karthik Gurusamy, Dec 9, 2007.

  1. I see python doesn't have ++ or -- operators unlike say, C.
    I read some reasonings talking about immutable scalars and using ++/--
    doesn't make much sense in python (not sure if ++i is that far-fetched
    compared to the allowed i += 1)

    In any case, I accidentally wrote ++n in python and it silently
    accepted the expression and it took me a while to debug the problem.

    Why are the following accepted even without a warning about syntax
    error?
    (I would expect the python grammar should catch these kind of syntax
    errors)

    >>> n = 1
    >>> 2 * +++++ n

    2
    >>> n += 1
    >>> n

    2
    >>> ++n

    2

    Karthik
     
    Karthik Gurusamy, Dec 9, 2007
    #1
    1. Advertising

  2. Karthik Gurusamy

    Chris M Guest

    On Dec 8, 7:58 pm, Karthik Gurusamy <> wrote:
    > I see python doesn't have ++ or -- operators unlike say, C.
    > I read some reasonings talking about immutable scalars and using ++/--
    > doesn't make much sense in python (not sure if ++i is that far-fetched
    > compared to the allowed i += 1)
    >
    > In any case, I accidentally wrote ++n in python and it silently
    > accepted the expression and it took me a while to debug the problem.
    >
    > Why are the following accepted even without a warning about syntax
    > error?
    > (I would expect the python grammar should catch these kind of syntax
    > errors)
    >
    > >>> n = 1
    > >>> 2 * +++++ n

    > 2
    > >>> n += 1
    > >>> n

    > 2
    > >>> ++n

    >
    > 2
    >
    > Karthik


    There is a unary operator "+". When you write ++n, it's evaluating +n,
    and then +(that result).
     
    Chris M, Dec 9, 2007
    #2
    1. Advertising

  3. On Sat, 08 Dec 2007 16:58:25 -0800, Karthik Gurusamy wrote:

    > Why are the following accepted even without a warning about syntax
    > error?
    > (I would expect the python grammar should catch these kind of syntax
    > errors)
    >
    >>>> n = 1
    >>>> 2 * +++++ n

    > 2
    >>>> n += 1
    >>>> n

    > 2
    >>>> ++n

    > 2


    There is no syntax error. It is just some unary pluses "chained". Maybe
    unexpected but no syntax error.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Dec 9, 2007
    #3
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,077
    Smokey Grindel
    Dec 2, 2006
  2. Replies:
    104
    Views:
    11,081
    Jordan Abel
    Oct 28, 2005
  3. Replies:
    99
    Views:
    2,533
    eliza81
    Jun 11, 2010
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    914
    Alf P. Steinbach /Usenet
    May 22, 2011
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    568
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page