Why is there no post-pre increment operator in python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • riteshtijoriwala@gmail.com

    #1

    Why is there no post-pre increment operator in python

    Anyone has any idea on why is there no post/pre increment operators in
    python ?
    Although the statement:
    ++j
    works but does nothing

  • Roy Smith

    #2
    Re: Why is there no post-pre increment operator in python

    riteshtijoriwal a@gmail.com wrote:[color=blue]
    > Anyone has any idea on why is there no post/pre increment operators in
    > python ?[/color]

    Short answer: Because Guido didn't like them.

    Longer answer: Because they encourage people to write cryptic one-liners.
    There really isn't anything you can't write with them that you couldn't
    write just as well without them. It just takes another line or two of
    code. The end result may be a little longer, but it's almost always easier
    to understand.
    [color=blue]
    > Although the statement:
    > ++j
    > works but does nothing[/color]

    Well, it works in the sense that it's not a syntax error, but it doesn't
    quite do nothing. It applies the unary + operator to the value of j, then
    does it again, then throws away the result. Granted, that's probably not
    what you expected, and probably not very useful, but it's not quite
    "nothing".

    Comment

    • Tim Peters

      #3
      Re: Why is there no post-pre increment operator in python

      [riteshtijoriwal a@gmail.com][color=blue]
      > Anyone has any idea on why is there no post/pre increment operators in
      > python ?[/color]

      Maybe because Python doesn't aim at being a cryptic portable assembly
      language? That's my guess ;-)
      [color=blue]
      > Although the statement:
      > ++j
      > works but does nothing[/color]

      That depends on the type of j, and how it implements the __pos__()
      method. The builtin numeric types (integers, floats, complex)
      implement __pos__ to return the base-class part of `self`. That's not
      the same as doing nothing. There is no "++" operator in Python, BTW
      -- that's two applications of the unary-plus operator.
      [color=blue][color=green][color=darkred]
      >>> class MyFloat(float):[/color][/color][/color]
      .... pass[color=blue][color=green][color=darkred]
      >>> x = MyFloat(3.5)
      >>> x[/color][/color][/color]
      3.5[color=blue][color=green][color=darkred]
      >>> type(x)[/color][/color][/color]
      <class '__main__.MyFlo at'>[color=blue][color=green][color=darkred]
      >>> type(+x) # "downcasts" to base `float` type[/color][/color][/color]
      <type 'float'>[color=blue][color=green][color=darkred]
      >>> type(x.__pos__( )) # same thing, but wordier[/color][/color][/color]
      <type 'float'>

      If you want, you can implement __pos__ in your class so that

      +a_riteshtijori wala_object

      posts messages to comp.lang.c asking why C is so inflexible ;-).

      Comment

      • Mike Meyer

        #4
        Re: Why is there no post-pre increment operator in python

        riteshtijoriwal a@gmail.com writes:[color=blue]
        > Anyone has any idea on why is there no post/pre increment operators in
        > python ?[/color]

        For lots of good reasons.
        [color=blue]
        > Although the statement:
        > ++j
        > works but does nothing[/color]

        So does --j. They both parse as a value with two unary operators
        applied to it in succession: +(+(j)) and -(-(j)).

        <mike
        --
        Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
        Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

        Comment

        • gene tani

          #5
          Re: Why is there no post-pre increment operator in python


          riteshtijoriwal a@gmail.com wrote:[color=blue]
          > Anyone has any idea on why is there no post/pre increment operators in
          > python ?
          > Although the statement:
          > ++j
          > works but does nothing[/color]

          "+=1" and "-=1" inflate your KLOC by .001, but they always work as
          expected with integers, it's when you do augmented assignments on lists
          and tuples that shit happens



          Comment

          • Peter Hansen

            #6
            Re: Why is there no post-pre increment operator in python

            riteshtijoriwal a@gmail.com wrote:[color=blue]
            > Anyone has any idea on why is there no post/pre increment operators in
            > python ?
            > Although the statement:
            > ++j
            > works but does nothing[/color]

            The reason is pretty complex, but here it is: Python is not C.

            -Peter

            Comment

            • Roy Smith

              #7
              Python pitfalls

              In article <1137144220.391 281.321490@g43g 2000cwa.googleg roups.com>,
              "gene tani" <gene.tani@gmai l.com> wrote:
              [color=blue]
              > http://zephyrfalcon.org/labs/python_pitfalls.html[/color]

              Thanks for posting that URL; I hadn't seen the list before. Skimming over
              it, none of them really seemed noteworthy until I got to "5. Mutable
              default arguments", which rather shocked me. Good stuff to know!

              Comment

              • gene tani

                #8
                Why is there no post-pre increment operator in python


                Roy Smith wrote:[color=blue]
                > In article <1137144220.391 281.321490@g43g 2000cwa.googleg roups.com>,
                > "gene tani" <gene.tani@gmai l.com> wrote:
                >[color=green]
                > > http://zephyrfalcon.org/labs/python_pitfalls.html[/color]
                >
                > Thanks for posting that URL; I hadn't seen the list before. Skimming over
                > it, none of them really seemed noteworthy until I got to "5. Mutable
                > default arguments", which rather shocked me. Good stuff to know![/color]

                here's my full FAQ / gotcha list




                The official home of the Python Programming Language

                Learn Python programming language for free with our tutorials and code examples! Step-by-step tutorials is the best way to learn Python in 2026.

                Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology


                http://www.faqts.com/knowledge_base/index.phtml/fid/245


                pls don't hijack threads

                Comment

                • Peter Hansen

                  #9
                  Hijacking threads

                  gene tani wrote:[color=blue]
                  > Roy Smith wrote:[color=green]
                  >>Thanks for posting that URL; I hadn't seen the list before.[/color][/color]
                  [...][color=blue]
                  >
                  > pls don't hijack threads[/color]

                  Um, he didn't "hijack" it, he follow a tangent to the discussion and
                  even changed the Subject line in a very appropriate manner, both of are
                  completely acceptable netiquette and long-standing Usenet practices.

                  (Rather like I'm doing here.)

                  -Peter

                  Comment

                  • Fredrik Lundh

                    #10
                    Re: Why is there no post-pre increment operator in python

                    "gene tani" wrote:
                    [color=blue]
                    > pls don't hijack threads[/color]

                    this is usenet, not gene tani's web board.

                    if you have trouble dealing with subthreads, get a better news reader.

                    </F>



                    Comment

                    • gene tani

                      #11
                      Re: Hijacking threads


                      Peter Hansen wrote:[color=blue]
                      > gene tani wrote:[color=green]
                      > > Roy Smith wrote:[color=darkred]
                      > >>Thanks for posting that URL; I hadn't seen the list before.[/color][/color]
                      > [...][color=green]
                      > >
                      > > pls don't hijack threads[/color]
                      >
                      > Um, he didn't "hijack" it, he follow a tangent to the discussion and
                      > even changed the Subject line in a very appropriate manner, both of are
                      > completely acceptable netiquette and long-standing Usenet practices.
                      >
                      > (Rather like I'm doing here.)[/color]

                      Sorry, I was trying to be helpful. One thing, i think it helps to
                      glance over rejected PEPs every once in a while to reinforce what's not
                      there
                      This PEP contains the index of all Python Enhancement Proposals, known as PEPs. PEP numbers are assigned by the PEP editors, and once assigned are never changed. The version control history of the PEP texts represent their historical record.

                      [color=blue]
                      >
                      > -Peter[/color]

                      Gene

                      Comment

                      Working...