Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73295

Re: Python's numeric tower

References <539d0a40$0$29988$c3e8da3$5496439d@news.astraweb.com> <mailman.11068.1402818363.18130.python-list@python.org> <539dbcbe$0$29988$c3e8da3$5496439d@news.astraweb.com> <roy-671447.13284415062014@news.panix.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2014-06-15 11:45 -0600
Subject Re: Python's numeric tower
Newsgroups comp.lang.python
Message-ID <mailman.11071.1402854356.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Jun 15, 2014 at 11:28 AM, Roy Smith <roy@panix.com> wrote:
> In article <539dbcbe$0$29988$c3e8da3$5496439d@news.astraweb.com>,
>  Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
>
>> On Sun, 15 Jun 2014 01:22:50 -0600, Ian Kelly wrote:
>>
>> > On Sat, Jun 14, 2014 at 8:51 PM, Steven D'Aprano
>> > <steve+comp.lang.python@pearwood.info> wrote:
>> >> Does anyone know any examples of values or types from the standard
>> >> library or well-known third-party libraries which satisfies
>> >> isinstance(a, numbers.Number) but not isinstance(a, numbers.Complex)?
>> >
>> >>>> issubclass(decimal.Decimal, numbers.Number)
>> > True
>> >>>> issubclass(decimal.Decimal, numbers.Complex)
>> > False
>>
>> Well, that surprises and disappoints me, but thank you for the answer.
>
> Why would you expect Decimal to be a subclass of Complex?

One might expect it for the same reason that float is a subclass of
Complex.  Decimal was intentionally excluded from the numeric tower
(as noted in PEP 3141), but apparently it still subclasses Number.  As
I understand it the reason Decimal was excluded was because it doesn't
fully implement the methods of Complex; for example, given two Complex
instances, one expects to be able to add them, but that doesn't always
hold for Decimal depending on the type of the other operand:

>>> Decimal("2.0") + 2.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'decimal.Decimal' and 'float'

The Number ast doesn't specify any particular behavior and is there to
"to make it easy for people to be fuzzy about what kind of number they
expect", so I guess the developers so no harm in letting Decimal
subclass it.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python's numeric tower Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-06-15 02:51 +0000
  Re: Python's numeric tower Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-15 01:22 -0600
    Re: Python's numeric tower Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-06-15 15:33 +0000
      Re: Python's numeric tower Roy Smith <roy@panix.com> - 2014-06-15 13:28 -0400
        Re: Python's numeric tower Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-15 11:45 -0600
        Re: Python's numeric tower Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-06-16 00:38 +0000
          Re: Python's numeric tower Chris Angelico <rosuav@gmail.com> - 2014-06-16 11:02 +1000
            Re: Python's numeric tower Roy Smith <roy@panix.com> - 2014-06-15 21:57 -0400
              Re: Python's numeric tower Chris Angelico <rosuav@gmail.com> - 2014-06-16 12:04 +1000
                Re: Python's numeric tower Roy Smith <roy@panix.com> - 2014-06-15 22:17 -0400
                Re: Python's numeric tower Dan Sommers <dan@tombstonezero.net> - 2014-06-16 03:38 +0000
                Re: Python's numeric tower Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-06-16 09:29 +0100
                Re: Python's numeric tower Roy Smith <roy@panix.com> - 2014-06-15 23:48 -0400

csiph-web