[Python-Dev] Symmetry arguments for API expansion

Tim Peters tim.peters at gmail.com
Tue Mar 13 01:01:50 EDT 2018


[Tim. on as_integer_ratio()]
>> - I expect the audience is tiny.

[Alexander Belopolsky]
> The datetime module would benefit from having as_integer_ratio()
> supported by more types.  It's been hard to resist requests to allow
> Decimal in timedelta constructors and/or arithmetics

I don't see the connection.  That timedelta construction may use
as_integer_ratio() today doesn't mean it _has_ to use
as_integer_ratio() forever, and is no reason (to my mind) to add
as_integer_ratio all over the place.

Why not drop that, and in oddball cases see whether
fractions.Fraction() can handle the input?

>>> fractions.Fraction(decimal.Decimal("1.76"))
Fraction(44, 25)

Probably less efficient, but I don't care ;-)  And then, e.g.,
timedelta would also automagically allow Fraction arguments (which,
BTW, don't support as_integer_ratio() either).  Bonus:  if datetime is
bothering with hand-coding rational arithmetic now out of concern to
get every bit right, Fraction could handle that too by itself.

At heart, the Fraction() constructor is _all about_ creating integer
ratios, so is the most natural place to put knowledge of how to do so.
A protocol for allowing new numeric types to get converted to Fraction
would be more generally useful than just a weird method only datetime
uses ;-)


More information about the Python-Dev mailing list