New submission from Robert Smallshire:
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.
----------
components: Library (Lib)
files: is_integer_numeric_tower.patch
keywords: patch
messages: 262702
nosy: Robert Smallshire2
priority: normal
severity: normal
status: open
title: Incorporating float.is_integer into the numeric tower and Decimal
type: enhancement
Added file: http://bugs.python.org/file42335/is_integer_numeric_tower.patch
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue26680>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com