Re: Python ++ Operator?

Discussion in 'Python' started by Chris Angelico, Jul 15, 2011.

  1. 2011/7/15 Rafael Durán Castañeda <>:
    > Hello all,
    > What's the meaning of using i++? Even, does exist ++ operator in python?


    ++i is legal Python but fairly useless. It's the unary + operator,
    applied twice. It doesn't increment the variable.

    Now, i+=1 IS valid Python, and WILL do what a C programmer expects it to.

    ChrisA
     
    Chris Angelico, Jul 15, 2011
    #1
    1. Advertising

  2. Chris Angelico

    Chris Torek Guest

    In article <>
    Chris Angelico <> wrote:
    >2011/7/15 Rafael Durán Castañeda <>:
    >> Hello all,
    >> What's the meaning of using i++? Even, does exist ++ operator in python?

    >
    >++i is legal Python but fairly useless. It's the unary + operator,
    >applied twice. It doesn't increment the variable.


    Well...

    class Silly:
    def __init__(self, value):
    self.value = value
    self._pluscount = 0
    def __str__(self):
    return str(self.value)
    def __pos__(self):
    self._pluscount += 1
    if self._pluscount == 2:
    self.value += 1
    self._pluscount = 0
    return self

    def main():
    i = Silly(0)
    print('initially, i = %s' % i)
    print('plus-plus i = %s' % ++i)
    print('finally, i = %s' % i)

    main()

    :)

    (Of course, +i followed by +i *also* increments i...)
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Intel require I note that my opinions are not those of WRS or Intel
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: gmail (figure it out) http://web.torek.net/torek/index.html
     
    Chris Torek, Jul 15, 2011
    #2
    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. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    612
    Rob Williscroft
    Mar 5, 2004
  2. John Smith
    Replies:
    2
    Views:
    441
    Ivan Vecerina
    Oct 6, 2004
  3. Alex Vinokur
    Replies:
    4
    Views:
    3,074
    Peter Koch Larsen
    Nov 26, 2004
  4. Alex Vinokur
    Replies:
    3
    Views:
    5,056
    Jeff Schwab
    Mar 20, 2005
  5. Tim Clacy
    Replies:
    15
    Views:
    2,737
    Kanenas
    May 30, 2005
Loading...

Share This Page