Issue 1562: Decimal can't be subclassed useful
Issue1562
Created on 2007-12-06 14:13 by poelzi, last changed 2022-04-11 14:56 by admin. This issue is now closed.
| Messages (4) | |||
|---|---|---|---|
| msg58241 - (view) | Author: (poelzi) | Date: 2007-12-06 14:13 | |
The Decimal class doesn't use lookups through self to construct results in functions like __add__ to generate the resulting object. This makes subclassing Decimal more or less senseless since all methods have to be wrapped instead of overriding the __new__ and __init__ methods, which could be enough for immutable type. Currently I'm implementing a Money class which is more or less a Decimal with addition currency information. Because resulting Types generated with something like return Decimal(something) instead of self.__new__(...) |
|||
| msg58276 - (view) | Author: Christian Heimes (christian.heimes) * ![]() |
Date: 2007-12-07 13:47 | |
Can you create a patch that replaces Decimal with self.__class__ and the string "Decimal" with "%s ..." % self.__class__.__name__? Thanks :) |
|||
| msg58288 - (view) | Author: Mark Dickinson (mark.dickinson) * ![]() |
Date: 2007-12-08 00:46 | |
It's not clear to me that this would be the right behaviour. Unless I'm missing something, Decimal behaves in just the same way as types like int, float and str in this respect: >>> class myint(int): pass ... >>> a = myint(2) >>> b = myint(3) >>> a+b 5 >>> type(_) <type 'int'> Tim Peters had something to say on this subject at: http://mail.python.org/pipermail/python-list/2005-January/300791.html |
|||
| msg59442 - (view) | Author: Facundo Batista (facundobatista) * ![]() |
Date: 2008-01-07 12:21 | |
Mark is right, the current behaviour is correct. See Tim post for a longer explanation. As general help, for the money class take a look of this: http://sourceforge.net/projects/pymoney There you'll see that the approach was to store the value, not subclass Decimal, as the other currency info makes math ugly. For example: Money("15.30", currency="USD") + Money("2.33", currency="ARS") There's a lot of discussion for a Money data type, also, in python-list and python-dev (I even proposed a pre-PEP), but then Decimal was implemented; check those lists at the fourth quarter of 2003 for these posts. Regards, |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:56:28 | admin | set | github: 45903 |
| 2008-01-07 12:21:21 | facundobatista | set | status: open -> closed resolution: wont fix messages: + msg59442 |
| 2007-12-08 00:46:03 | mark.dickinson | set | nosy:
+ mark.dickinson messages: + msg58288 |
| 2007-12-07 13:47:28 | christian.heimes | set | versions:
+ Python 2.6, Python 3.0 nosy: + christian.heimes, facundobatista messages: + msg58276 priority: normal assignee: facundobatista type: behavior |
| 2007-12-06 14:13:37 | poelzi | create | |
