Integer object whose value can be changed after definition?
<p>I need a class that is an integer whose value can be changed after the object is created. 
I need this class to define a size that is first specified in millimeters. Later when the user-interface is created, I get a factor from the device-context that converts millimeters to pixels. This factor should change the millimeter-value of my object to a pixel-value. </p>

<p>I tried to subclass int (see <a href="http://stackoverflow.com/questions/1135335/increment-int-object">increment int object</a>), but int is immutable so I can't change its value. </p>

<pre><code>class UiSize(int):
    def __new__(cls, value=0):
        i = int.__new__(cls, value)
        i._orig_value = value
        return i

    def set_px_per_mm(self, px_per_mm):
        pixel_value = int(round(self._orig_value * px_per_mm))
        print "pixel_value", pixel_value
        # how to set the new pixel_value to the object's value ?

s = UiSize(500)
s.set_px_per_mm(300.0 / 500.0)
print "1px + 500mm =", 1 + s, "px" # the result should be 301 pixels
</code></pre>

<p>The answer in <a href="http://stackoverflow.com/questions/1135335/increment-int-object">increment int object</a> was 
to build my own class with all the int's methods. So I tried this:</p>

<pre><code>class UiSize2(object):
    def __init__(self, value=0):
        self._int_value = int(value)

    def __add__(self, other):
        return self._int_value.__add__(other)

    def set_px_per_mm(self, px_per_mm):
        self._int_value = int(round(self._int_value * px_per_mm))

s = UiSize2(500)
s.set_px_per_mm(300.0 / 500.0)
print "500mm + 1 =", s + 1, "px"
</code></pre>

<p>I works for 's + 1', but for '1 + s' I get a TypeError:</p>

<pre><code>&gt;&gt;&gt; print "1 + 500mm =", 1 + s, "px"
TypeError: unsupported operand type(s) for +: 'int' and 'UiSize2'
</code></pre>
