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

Skip to content

Commit 175ba36

Browse files
authored
gh-72902: improve Fraction constructor speed for typical inputs (GH-134320)
This moves abc check for numbers.Rational - down.
1 parent e007e62 commit 175ba36

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

Lib/fractions.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,6 @@ def __new__(cls, numerator=0, denominator=None):
238238
self._denominator = 1
239239
return self
240240

241-
elif isinstance(numerator, numbers.Rational):
242-
self._numerator = numerator.numerator
243-
self._denominator = numerator.denominator
244-
return self
245-
246241
elif (isinstance(numerator, float) or
247242
(not isinstance(numerator, type) and
248243
hasattr(numerator, 'as_integer_ratio'))):
@@ -278,6 +273,11 @@ def __new__(cls, numerator=0, denominator=None):
278273
if m.group('sign') == '-':
279274
numerator = -numerator
280275

276+
elif isinstance(numerator, numbers.Rational):
277+
self._numerator = numerator.numerator
278+
self._denominator = numerator.denominator
279+
return self
280+
281281
else:
282282
raise TypeError("argument should be a string or a Rational "
283283
"instance or have the as_integer_ratio() method")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve speed (x1.1-1.8) of the :class:`~fractions.Fraction` constructor for
2+
typical inputs (:class:`float`'s, :class:`~decimal.Decimal`'s or strings).

0 commit comments

Comments
 (0)