On Mon, Mar 12, 2018 at 1:03 PM, Raymond Hettinger <
raymond.hettin...@gmail.com> wrote:

> > On Mar 12, 2018, at 12:15 PM, Guido van Rossum <gu...@python.org> wrote:
> >
> > There's a reason why adding this to int feels right to me. In mypy we
> treat int as a sub*type* of float, even though technically it isn't a
> sub*class*. The absence of an is_integer() method on int means that this
> code has a bug that mypy doesn't catch:
> >
> > def f(x: float):
> >     if x.is_integer():
> >         "do something"
> >     else:
> >         "do something else"
> >
> > f(12)
> Do you have any thoughts about the other non-corresponding float methods?

Not really, but I'll try below.

>     >>> set(dir(float)) - set(dir(int))
>    {'as_integer_ratio', 'hex', '__getformat__', 'is_integer',
> '__setformat__', 'fromhex'}

IIUC fromhex is a class method so the story isn't the same there -- typical
use is float.fromhex(). as_integer_ratio() seems mostly cute (it has Tim
Peters all over it), OTOH it looks like Decimal has it, so I think this
ship has sailed too and maybe it's best to add it to the numeric tower just
to be done with it.

I found a comment for __getformat__ saying "You probably don't want to use
this function. It exists mainly to be used in Python's test suite" so let's
skip that.

So that leaves hex(). There I think it's preposterous that for ints you
have to write hex(i) but for floats you must write x.hex(). The idea that
the user always knows whether they have an int or a float is outdated (it
stems back to the very early Python days when 3.14 + 42 was a type error --
Tim talked me out of that in '91 or '92). If you force me to choose between
allowing hex(3.14) or 42.hex() I'll choose the latter -- we also have
bytes.hex() and it's an easier change to add a hex() method to int than to
extend the hex() function -- we'd have to add a __hex__ protocol first.

> In general, would you prefer that functionality like is_integer() be a
> math module function or that is should be a method on all numeric types
> except Complex?  I expect questions like this to recur over time.

That feels like a loaded question -- we have a math module because C has
one and back in 1990 I didn't want to spend time thinking about such design

> Also, do you have any thoughts on the feature itself?  Serhiy ran a Github
> search and found that it was baiting people into worrisome code like:
> (x/5).is_integer() or (x**0.5).is_integer()

Finding bad example of floating point use is like stealing candy from
babies. The feature seems venerable so I think there would have to be a
very high bar to deprecate it -- I don't think you want to go there.

> > So I think the OP of the bug has a valid point, 27 years without this
> feature notwithstanding.
> Okay, I'll ask the OP to update his patch :-)

--Guido van Rossum (python.org/~guido)
Python-Dev mailing list

Reply via email to