Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #9521 > unrolled thread

Re: Python ++ Operator?

Started byChris Angelico <rosuav@gmail.com>
First post2011-07-15 17:55 +1000
Last post2011-07-15 20:48 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Python ++ Operator? Chris Angelico <rosuav@gmail.com> - 2011-07-15 17:55 +1000
    Re: Python ++ Operator? Chris Torek <nospam@torek.net> - 2011-07-15 20:48 +0000

#9521 — Re: Python ++ Operator?

FromChris Angelico <rosuav@gmail.com>
Date2011-07-15 17:55 +1000
SubjectRe: Python ++ Operator?
Message-ID<mailman.1055.1310716536.1164.python-list@python.org>
2011/7/15 Rafael Durán Castañeda <rafadurancastaneda@gmail.com>:
> 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

[toc] | [next] | [standalone]


#9565

FromChris Torek <nospam@torek.net>
Date2011-07-15 20:48 +0000
Message-ID<ivq93e01crm@news1.newsguy.com>
In reply to#9521
In article <mailman.1055.1310716536.1164.python-list@python.org>
Chris Angelico  <rosuav@gmail.com> wrote:
>2011/7/15 Rafael Durán Castañeda <rafadurancastaneda@gmail.com>:
>> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web