Hello Python gurus!

I need to subclass the built-in type "int" and to add a method that resets the value to what it was when the class was first instantiated.
here is what i want to achieve:

a = my_int(4)
print a ---> prints 4 (works ok)
a += 5
print a ---> prints 9 (works ok)
a.reset()
print a ---> doesnt work


Here is how far i got in creating this class:

class my_int(int):
    def __new__(cls,arg=0):
        __v = arg
        return int.__new__(cls,arg)
    def reset(self):
        int.__init__(self, __v)    ---> this doesnt work. neither does "self = __v" .

so how do i set the value of the int (parent class) from within the method ?
thanks!

Int objects are immutable. The statement a+=5 creates a new object which is an int and not an instance of your class. That is why your a.reset() call does not work.

Try playing around type(a) and id(a) in the different stages.

What are you trying to accomplish? Or whas that only a theoretical question?

Int objects are immutable. The statement a+=5 creates a new object which is an int and not an instance of your class. That is why your a.reset() call does not work.

do you mean that every operation involving assignment creates a new instance and the previous instance of the "variable" is garbage collected ?

Try playing around type(a) and id(a) in the different stages.
What are you trying to accomplish? Or whas that only a theoretical question?

I would like to have my_int, my_float, etc. types with the rich functionality that they already have and add some proprietary features such as reset() that restores the original instantiated value.. or xyz() that does whatever and stores a new value into the the my_int instance. I would prefer not to have to create a new my_int class from scratch and overload all the "int" operators one by one.

is there a method i've missed like __assign__() that implements the assignment operator ? that i can call from within the sub-classed method?

do you mean that every operation involving assignment creates a new instance and the previous instance of the "variable" is garbage collected ?

No. Assignment does not create a new variable but gives a name (that is on the left side) to an existing or newly created object(right side).
However a+=6 is not an assignment but an operator, too. Roughly it means c=a+6; a=c. That means "c" is created assigned to "a" and the old value of "a" is garbage.

I would like to have my_int, my_float, etc. types with the rich functionality that they already have and add some proprietary features

So a+=2 should be eqivalent to a=my_int(a.__v+2, original=a._v)?
Then a=2+a should be equivalent to what? I do not get it.


No object has a magic "value" attribute. If I say a=b+c, then I call b.__add__(c) and get usually a new object. I can override this method, but then I should override all operator methods I want to use in my code.

I don't (and not qualified enough to) go in further details why it is not fortunate to redefine an immutable object into a mutable one. But I am pretty sure it is not a good idea. By the way all operators on all builtin types I know create new objects. Even on mutable ones.

If you want to memoize a value, then you cannot store it in an immutable object, that is the point.

If you want to define a "small" object, you can always use __slots__, for example you could define

class my_int(object):
    __slots__ = ["value", "initial"]
    def __init__(self, i=0):
        self.value = self.initial = int(i)
    def __int__(self):
        return self.value
    def reset(self):
        self.value = self.initial
    # etc
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.