Skip to content

Commit f9bfbf3

Browse files
author
raymond.hettinger
committed
Register decimals as numbers.Number
git-svn-id: http://svn.python.org/projects/python/trunk@69242 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent dea9c86 commit f9bfbf3

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

Lib/decimal.py

+7
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136

137137
import copy as _copy
138138
import math as _math
139+
import numbers as _numbers
139140

140141
try:
141142
from collections import namedtuple as _namedtuple
@@ -3608,6 +3609,12 @@ def _dec_from_triple(sign, coefficient, exponent, special=False):
36083609

36093610
return self
36103611

3612+
# Register Decimal as a kind of Number (an abstract base class).
3613+
# However, do not register it as Real (because Decimals are not
3614+
# interoperable with floats).
3615+
_numbers.Number.register(Decimal)
3616+
3617+
36113618
##### Context class #######################################################
36123619

36133620

Lib/test/test_decimal.py

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import pickle, copy
3131
import unittest
3232
from decimal import *
33+
import numbers
3334
from test.test_support import (TestSkipped, run_unittest, run_doctest,
3435
is_resource_enabled)
3536
import random
@@ -1334,6 +1335,12 @@ def test_implicit_context(self):
13341335

13351336
class DecimalPythonAPItests(unittest.TestCase):
13361337

1338+
def test_abc(self):
1339+
self.assert_(issubclass(Decimal, numbers.Number))
1340+
self.assert_(not issubclass(Decimal, numbers.Real))
1341+
self.assert_(isinstance(Decimal(0), numbers.Number))
1342+
self.assert_(not isinstance(Decimal(0), numbers.Real))
1343+
13371344
def test_pickle(self):
13381345
d = Decimal('-3.141590000')
13391346
p = pickle.dumps(d)

0 commit comments

Comments
 (0)