[Python-Dev] ++x oddnes

Skip Montanaro skip@pobox.com (Skip Montanaro)
Mon, 13 Aug 2001 10:09:43 -0500

    >> I don't see any incrementing going on...

    Fredrik> try this:

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

    Fredrik> c = StupidCounter()
    Fredrik> print c
    Fredrik> ++c
    Fredrik> print c

Thanks, I understand now.  Of course +c also increments the counter...