[Python-Dev] Symmetry arguments for API expansion

Raymond Hettinger raymond.hettinger at gmail.com
Mon Mar 12 16:03:42 EDT 2018


> On Mar 12, 2018, at 12:15 PM, Guido van Rossum <guido at 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?

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

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.

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()

> 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 :-)


Raymond


More information about the Python-Dev mailing list