This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Decimal can't be subclassed useful
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.0, Python 2.6, Python 2.5
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: facundobatista Nosy List: christian.heimes, facundobatista, mark.dickinson, poelzi
Priority: normal Keywords:

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 
msg58276 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) 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) * (Python committer) 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
>>> type(_)
<type 'int'>

Tim Peters had something to say on this subject at:
msg59442 - (view) Author: Facundo Batista (facundobatista) * (Python committer) 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:

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

Date User Action Args
2022-04-11 14:56:28adminsetgithub: 45903
2008-01-07 12:21:21facundobatistasetstatus: open -> closed
resolution: wont fix
messages: + msg59442
2007-12-08 00:46:03mark.dickinsonsetnosy: + mark.dickinson
messages: + msg58288
2007-12-07 13:47:28christian.heimessetversions: + Python 2.6, Python 3.0
nosy: + christian.heimes, facundobatista
messages: + msg58276
priority: normal
assignee: facundobatista
type: behavior
2007-12-06 14:13:37poelzicreate