Behaviour of increment and decrement operators in Python

后端 未结 9 1991
无人共我
无人共我 2020-11-22 05:26

I notice that a pre-increment/decrement operator can be applied on a variable (like ++count). It compiles, but it does not actually change the value of the vari

9条回答
  •  爱一瞬间的悲伤
    2020-11-22 05:52

    Yeah, I missed ++ and -- functionality as well. A few million lines of c code engrained that kind of thinking in my old head, and rather than fight it... Here's a class I cobbled up that implements:

    pre- and post-increment, pre- and post-decrement, addition,
    subtraction, multiplication, division, results assignable
    as integer, printable, settable.
    

    Here 'tis:

    class counter(object):
        def __init__(self,v=0):
            self.set(v)
    
        def preinc(self):
            self.v += 1
            return self.v
        def predec(self):
            self.v -= 1
            return self.v
    
        def postinc(self):
            self.v += 1
            return self.v - 1
        def postdec(self):
            self.v -= 1
            return self.v + 1
    
        def __add__(self,addend):
            return self.v + addend
        def __sub__(self,subtrahend):
            return self.v - subtrahend
        def __mul__(self,multiplier):
            return self.v * multiplier
        def __div__(self,divisor):
            return self.v / divisor
    
        def __getitem__(self):
            return self.v
    
        def __str__(self):
            return str(self.v)
    
        def set(self,v):
            if type(v) != int:
                v = 0
            self.v = v
    

    You might use it like this:

    c = counter()                          # defaults to zero
    for listItem in myList:                # imaginary task
         doSomething(c.postinc(),listItem) # passes c, but becomes c+1
    

    ...already having c, you could do this...

    c.set(11)
    while c.predec() > 0:
        print c
    

    ....or just...

    d = counter(11)
    while d.predec() > 0:
        print d
    

    ...and for (re-)assignment into integer...

    c = counter(100)
    d = c + 223 # assignment as integer
    c = c + 223 # re-assignment as integer
    print type(c),c #  323
    

    ...while this will maintain c as type counter:

    c = counter(100)
    c.set(c + 223)
    print type(c),c #  323
    

    EDIT:

    And then there's this bit of unexpected (and thoroughly unwanted) behavior,

    c = counter(42)
    s = '%s: %d' % ('Expecting 42',c) # but getting non-numeric exception
    print s
    

    ...because inside that tuple, getitem() isn't what used, instead a reference to the object is passed to the formatting function. Sigh. So:

    c = counter(42)
    s = '%s: %d' % ('Expecting 42',c.v) # and getting 42.
    print s
    

    ...or, more verbosely, and explicitly what we actually wanted to happen, although counter-indicated in actual form by the verbosity (use c.v instead)...

    c = counter(42)
    s = '%s: %d' % ('Expecting 42',c.__getitem__()) # and getting 42.
    print s
    

提交回复
热议问题