Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-72902: improve Fraction constructor speed for typical inputs #134320

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

Merged
merged 1 commit into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 5 additions & 5 deletions Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,6 @@ def __new__(cls, numerator=0, denominator=None):
self._denominator = 1
return self

elif isinstance(numerator, numbers.Rational):
self._numerator = numerator.numerator
self._denominator = numerator.denominator
return self

elif (isinstance(numerator, float) or
(not isinstance(numerator, type) and
hasattr(numerator, 'as_integer_ratio'))):
Expand Down Expand Up @@ -278,6 +273,11 @@ def __new__(cls, numerator=0, denominator=None):
if m.group('sign') == '-':
numerator = -numerator

elif isinstance(numerator, numbers.Rational):
self._numerator = numerator.numerator
self._denominator = numerator.denominator
return self

else:
raise TypeError("argument should be a string or a Rational "
"instance or have the as_integer_ratio() method")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve speed (x1.1-1.8) of the :class:`~fractions.Fraction` constructor for
typical inputs (:class:`float`'s, :class:`~decimal.Decimal`'s or strings).
Loading