subclassing Decimal

jbauer at rubic.com jbauer at rubic.com
Mon Feb 21 16:44:24 EST 2005


I was interested in playing around with Decimal and
subclassing it.  For example, if I wanted a special
class to permit floats to be automatically converted
to strings.

    from decimal import Decimal

    class MyDecimal(Decimal):
        def __init__(self, value):
            if isinstance(value, float):
                ... initialize using str(float) ...

In the classic days, I would have added something
like this to MyDecimal __init__:

    Decimal.__init__(self, str(value))

But I'm unfamiliar with the __new__ protocol.

Jeff Bauer
Rubicon, Inc.




More information about the Python-list mailing list