Usefulness of subclassing builtin number types

Skip Montanaro skip at pobox.com
Sun Dec 15 18:01:51 EST 2002


    > And I want (of course) that this subclassing is 'sticky', so if I do a:
    >     x = MyInt(25)
    >     x *= 2

    > I want 'x' to stay of the class MyInt.

This works for me:

    class MyInt(int):
        def __imul__(self, other):
            return MyInt(self*other)

    x = MyInt(25)
    x *= 2
    print type(x), x

-- 
Skip Montanaro - skip at pobox.com
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list