Author robert_smallshire
Recipients robert_smallshire
Date 2016-03-31.19:34:21
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1459452864.35.0.120403756979.issue26680@psf.upfronthosting.co.za>
In-reply-to
Content
When the useful float.is_integer method was added the opportunity was missed to incorporate this method into the numeric tower defined in numbers.py. This increased the API distance between different number types, making them less substitutable than previously, leading to what might be considered to be absurd behaviour:

  >>> a = 5.0
  >>> b = 5
  >>> a.is_integer()
  True
  >>> b.is_integer()
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  AttributeError: 'int' object has no attribute 'is_integer'

The first attached patch modifies Python to:

 1) Implement int.is_integer() to always return True
 2) Add Real.is_integer() as an abstract method in numbers.py
 3) Provide a default implementation in Rational.is_integer() in numbers.py
 4) Adds tests for is_integer() for int and Fraction.
 5) Documentation changes commensurate with above.

Although the Decimal type deliberately lies outside the numeric tower for reasons not relevant here, the principle of least surprise suggests that it too should support is_integer().  In fact, the implementation already contains just such a function, although it is not exposed to Python.  The second patch implements is_integer() for both the pure Python and C implementations of Decimal, again with commensurate tests and documentation changes.

I hope these changes can be implemented to reduce the degree of surprise encountered when working with different number types in Python.
History
Date User Action Args
2016-03-31 19:34:25robert_smallshiresetrecipients: + robert_smallshire
2016-03-31 19:34:24robert_smallshiresetmessageid: <1459452864.35.0.120403756979.issue26680@psf.upfronthosting.co.za>
2016-03-31 19:34:24robert_smallshirelinkissue26680 messages
2016-03-31 19:34:23robert_smallshirecreate