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.

classification
Title: decimal.py: New instance vs. new reference
Type: behavior Stage:
Components: Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, rhettinger, skrah
Priority: normal Keywords:

Created on 2009-11-07 09:51 by skrah, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg95015 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2009-11-07 09:51
In the following case, Decimal() and int() behave differently. I wonder
if this is intentional:

>>> from decimal import *
>>> x = Decimal(2)
>>> y = Decimal(x)
>>> id(x) == id(y)
False
>>> 
>>> x = int(2)
>>> y = int(x)
>>> id(x) == id(y)
True
>>>
msg95030 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-11-08 00:14
I don't think it should matter whether either 'id(x) == id(y)' returns 
True or False:  since both ints and Decimals are regarded as immutable, 
the implementation should be free to reuse (or not) existing objects at 
will.

There is a test in test_decimal.py that seems to expect that Decimal(x) 
always returns a copy.  I'm not quite sure why that test is there, 
though it may be because Decimal instances weren't always as immutable 
as they are now (some of the non-__new__ methods used to modify the 
Decimal fields directly).  In my opinion that test could be removed, but 
I'd like to consult with some of the other Decimal authors before doing 
so.

(An aside: it's important to note that operations on Decimal subclasses 
should always return *Decimal* instances, not instances of the subclass.
So any code like:

  if isinstance(x, Decimal): return x

should be regarded with suspicion.)

N.B. Some of your recent reports seem more like questions about the 
implementation rather than reports of possible bugs in Python;  I'm not 
sure that the bug tracker is the best medium for this.  python-dev might 
be more appropriate, or I'm happy to answer private emails.
msg95037 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-11-08 06:40
Stefan, this is a non-guaranteed implementation detail (for both ints
and decimals).
History
Date User Action Args
2022-04-11 14:56:54adminsetgithub: 51527
2009-11-08 06:40:38rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg95037

resolution: not a bug
2009-11-08 00:14:44mark.dickinsonsetmessages: + msg95030
2009-11-07 20:42:31skrahsettype: behavior
2009-11-07 09:51:27skrahcreate