homepage

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.

Author serhiy.storchaka
Recipients facundobatista, mark.dickinson, rhettinger, serhiy.storchaka, skrah
Date 2016年05月12日.07:15:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1463037344.61.0.425475414297.issue27006@psf.upfronthosting.co.za>
In-reply-to
Content
Python implementation of Decimal.from_float() calls __new__ and __init__ methods of subclass.
>>> from _pydecimal import Decimal
>>> class D(Decimal):
... def __new__(cls, *args, **kwargs):
... print('__new__')
... return Decimal.__new__(cls, *args, **kwargs)
... def __init__(self, *args, **kwargs):
... print('__init__')
... 
>>> print(type(D.from_float(42)))
__new__
__init__
<class '__main__.D'>
>>> print(type(D.from_float(42.0)))
__new__
__init__
<class '__main__.D'>
But C implementation doesn't.
>>> from decimal import Decimal
>>> class D(Decimal):
... def __new__(cls, *args, **kwargs):
... print('__new__')
... return Decimal.__new__(cls, *args, **kwargs)
... def __init__(self, *args, **kwargs):
... print('__init__')
... 
>>> print(type(D.from_float(42)))
<class '__main__.D'>
>>> print(type(D.from_float(42.0)))
<class '__main__.D'>
This means that resulting instance of Decimal subclass can be in not valid state.
Example is Decimal enums (see also issue23640).
>>> from decimal import Decimal
>>> from enum import Enum
>>> class D(Decimal, Enum):
... A = Decimal('3.25')
... 
>>> D(Decimal(3.25))
<D.A: Decimal('3.25')>
>>> D(3.25)
<D.A: Decimal('3.25')>
>>> D.from_float(3.25)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/home/serhiy/py/cpython/Lib/enum.py", line 486, in __repr__
 self.__class__.__name__, self._name_, self._value_)
AttributeError: 'D' object has no attribute '_name_'
A solution is to reproduce Python implementation in C code:
 result = ... # create exact Decimal
 if cls is not Decimal:
 result = cls(result)
 return result
History
Date User Action Args
2016年05月12日 07:15:44serhiy.storchakasetrecipients: + serhiy.storchaka, rhettinger, facundobatista, mark.dickinson, skrah
2016年05月12日 07:15:44serhiy.storchakasetmessageid: <1463037344.61.0.425475414297.issue27006@psf.upfronthosting.co.za>
2016年05月12日 07:15:44serhiy.storchakalinkissue27006 messages
2016年05月12日 07:15:44serhiy.storchakacreate

AltStyle によって変換されたページ (->オリジナル) /