increment int object

int, python

Solution

ints are immutable, so you'll have to build your own class with all the int's methods if you want a "mutable int"

Problem

Is there a way in python to increment int object in place, int doesn't seem to implement `__iadd__` so += 1 actually returns a new object ``` >>> n=1 >>> id(n) 9788024 >>> n+=1 >>> id(n) 9788012 ``` What I want is n to remain pointing to same object. Purpose: I have class derived from int and I want to implement C type '++n' operator for that class Conclusion: ok as int is immutable there is no way, looks like i will have to write my own class something like this ``` class Int(object): def __init__(self, value): self._decr = False self.value = value def __neg__(self): if self._decr: self.value -= 1 self._decr = not self._decr return self def __str__(self): return str(self.value) def __cmp__(self, n): return cmp(self.value, n) def __nonzero__(self): return self.value n = Int(10) while --n: print n ```

Original source

Related problems