Why is there no post-pre increment operator in python

Discussion in 'Python' started by riteshtijoriwala@gmail.com, Jan 13, 2006.

  1. Guest

    Anyone has any idea on why is there no post/pre increment operators in
    python ?
    Although the statement:
    ++j
    works but does nothing
    , Jan 13, 2006
    #1
    1. Advertising

  2. Roy Smith Guest

    wrote:
    > Anyone has any idea on why is there no post/pre increment operators in
    > python ?


    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.

    > Although the statement:
    > ++j
    > works but does nothing


    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".
    Roy Smith, Jan 13, 2006
    #2
    1. Advertising

  3. Tim Peters Guest

    []
    > Anyone has any idea on why is there no post/pre increment operators in
    > python ?


    Maybe because Python doesn't aim at being a cryptic portable assembly
    language? That's my guess ;-)

    > Although the statement:
    > ++j
    > works but does nothing


    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.

    >>> class MyFloat(float):

    .... pass
    >>> x = MyFloat(3.5)
    >>> x

    3.5
    >>> type(x)

    <class '__main__.MyFloat'>
    >>> type(+x) # "downcasts" to base `float` type

    <type 'float'>
    >>> type(x.__pos__()) # same thing, but wordier

    <type 'float'>

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

    +a_riteshtijoriwala_object

    posts messages to comp.lang.c asking why C is so inflexible ;-).
    Tim Peters, Jan 13, 2006
    #3
  4. Mike Meyer Guest

    writes:
    > Anyone has any idea on why is there no post/pre increment operators in
    > python ?


    For lots of good reasons.

    > Although the statement:
    > ++j
    > works but does nothing


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

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Jan 13, 2006
    #4
  5. gene tani Guest

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


    "+=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

    http://zephyrfalcon.org/labs/python_pitfalls.html
    gene tani, Jan 13, 2006
    #5
  6. Peter Hansen Guest

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


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

    -Peter
    Peter Hansen, Jan 13, 2006
    #6
  7. Roy Smith Guest

    Python pitfalls

    In article <>,
    "gene tani" <> wrote:

    > http://zephyrfalcon.org/labs/python_pitfalls.html


    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!
    Roy Smith, Jan 13, 2006
    #7
  8. gene tani Guest

    Roy Smith wrote:
    > In article <>,
    > "gene tani" <> wrote:
    >
    > > http://zephyrfalcon.org/labs/python_pitfalls.html

    >
    > 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!


    here's my full FAQ / gotcha list

    http://www.ferg.org/projects/python_gotchas.html
    http://zephyrfalcon.org/labs/python_pitfalls.html
    http://zephyrfalcon.org/labs/beginners_mistakes.html
    http://www.python.org/doc/faq/
    http://diveintopython.org/appendix/abstracts.html
    http://www.onlamp.com/pub/a/python/2004/02/05/learn_python.html
    http://www.norvig.com/python-iaq.html
    http://www.faqts.com/knowledge_base/index.phtml/fid/245
    http://amk.ca/python/writing/warts

    pls don't hijack threads
    gene tani, Jan 13, 2006
    #8
  9. Peter Hansen Guest

    Hijacking threads

    gene tani wrote:
    > Roy Smith wrote:
    >>Thanks for posting that URL; I hadn't seen the list before.

    [...]
    >
    > pls don't hijack threads


    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
    Peter Hansen, Jan 13, 2006
    #9
  10. "gene tani" wrote:

    > pls don't hijack threads


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

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

    </F>
    Fredrik Lundh, Jan 13, 2006
    #10
  11. gene tani Guest

    Re: Hijacking threads

    Peter Hansen wrote:
    > gene tani wrote:
    > > Roy Smith wrote:
    > >>Thanks for posting that URL; I hadn't seen the list before.

    > [...]
    > >
    > > pls don't hijack threads

    >
    > 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.)


    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
    http://www.python.org/peps/

    >
    > -Peter


    Gene
    gene tani, Jan 13, 2006
    #11
    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. Andreas Sheriff
    Replies:
    10
    Views:
    714
    Jack Klein
    Sep 25, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,763
    Smokey Grindel
    Dec 2, 2006
  3. Replies:
    8
    Views:
    918
    Alf P. Steinbach
    Nov 22, 2007
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    880
    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:
    509
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page