8000 bpo-37836: support .as_integer_ratio() in Fraction by jdemeyer · Pull Request #15327 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-37836: support .as_integer_ratio() in Fraction #15327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
bpo-37836: minor fixes
  • Loading branch information
jdemeyer committed Aug 25, 2019
commit 9c7bdb5e057d990edb07031379efc7edfe19f886
5 changes: 3 additions & 2 deletions Doc/library/fractions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ or from a string.

.. versionchanged:: 3.2
The :class:`Fraction` constructor now accepts :class:`float` and
:class:`decimal.Decimal` instances.
:class:`decimal.Decimal` instances as a single argument.

.. versionchanged:: 3.9
The :class:`Fraction` constructor now accepts any object with
``as_integer_ratio()`` as numerator or denominator.
``as_integer_ratio()`` (in particular also :class:`float` and
:class:`decimal.Decimal` instances) as numerator or denominator.


.. attribute:: numerator
Expand Down
22 changes: 10 additions & 12 deletions Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,11 @@ def _as_integer_ratio(obj):
try:
f = obj.as_integer_ratio
except AttributeError:
pass
if isinstance(obj, numbers.Rational):
return (obj.numerator, obj.denominator)
else:
return f()

if isinstance(obj, numbers.Rational):
return (obj.numerator, obj.denominator)

return NotImplemented


Expand All @@ -82,20 +80,20 @@ class Fraction(numbers.Rational):
In the two-argument form of the constructor, Fraction(8, 6) will
produce a rational number equivalent to 4/3. The numerator defaults
to 0 and the denominator defaults to 1 so that Fraction(3) == 3 and
Fraction() == 0.
Fraction() == 0. The numerator and denominator can be:

- objects with an ``as_integer_ratio()`` method (this includes
integers, Fractions, floats and Decimal instances)

- other Rational instances

Fractions can be constructed from:
Fractions can also be constructed from a string (in this case, only
a single argument is allowed):

- numeric strings similar to those accepted by the
float constructor (for example, '-2.3' or '1e10')

- strings of the form '123/456'

- objects with an ``as_integer_ratio()`` method (this includes
float and Decimal instances)

- other Rational instances (including integers)

"""

__slots__ = ('_numerator', '_denominator')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
When constructing a :class:`fractions.Fraction`, the given numerator (and
optional denominator) may now be any object with an ``as_integer_ratio``
method.
When constructing a :class:`fractions.Fraction`, the given numerator and
denominator may now be any object with an ``as_integer_ratio`` method.
This allows for example passing floats for both the numerator and denominator.
0