[Python-Dev] ++x oddnes

Fredrik Lundh fredrik@pythonware.com
Mon, 13 Aug 2001 16:12:15 +0200


skip wrote:
> How?  ++x compiles to
>
>          LOAD_FAST         x
>          UNARY_POSITIVE
>          UNARY_POSITIVE
>
> I don't see any incrementing going on...

try this:

class StupidCounter:
    count = 0
    def __pos__(self):
        self.count = self.count + 1
        return 0 # ignore extra __pos__ calls
    def __int__(self):
        return self.count
    def __repr__(self):
        return repr(self.count)

c = StupidCounter()
print c
++c
print c

</F>