
This issue trackerhas been migrated toGitHub, and is currentlyread-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.
Created on2019-08-11 07:01 byrhettinger, last changed2022-04-11 14:59 byadmin. This issue is nowclosed.
| Pull Requests | |||
|---|---|---|---|
| URL | Status | Linked | Edit |
| PR 15212 | closed | rhettinger,2019-08-11 17:38 | |
| PR 15215 | merged | miss-islington,2019-08-11 21:41 | |
| Messages (11) | |||
|---|---|---|---|
| msg349378 -(view) | Author: Raymond Hettinger (rhettinger)*![]() | Date: 2019-08-11 07:01 | |
When working on Whatsnew3.8, I noticed that as_integer_ratio() had been added to ints and bools, was already present in floats and Decimals, but was missing from Fractions. IIRC, the goal was to make all of these have as a similar API so that x.as_integer_ratio() would work for all types that could support it (not complex numbers). | |||
| msg349381 -(view) | Author: Stefan Behnel (scoder)*![]() | Date: 2019-08-11 08:53 | |
FWIW, makes total sense to me to have it there. Question is more if we can still get it into Py3.8, since it's a new feature for fractions. | |||
| msg349397 -(view) | Author: Serhiy Storchaka (serhiy.storchaka)*![]() | Date: 2019-08-11 16:41 | |
This will not solve the problem completely, because other rational numbers do not have as_integer_ratio().But all rational numbers have the numerator and denominator properties. Seeissue37822 for more general solution. | |||
| msg349398 -(view) | Author: Raymond Hettinger (rhettinger)*![]() | Date: 2019-08-11 16:54 | |
I think this can still go in for Fractions because it completes a feature that was already started (providing the method for the concrete types where it makes sense).Altering numbers.py can be saved for the future. | |||
| msg349403 -(view) | Author: Raymond Hettinger (rhettinger)*![]() | Date: 2019-08-11 18:00 | |
Guido, would you like to go forward with this?IIRC, you had decided that adding as_integer_ratio() to all the relevant concrete classes was the way to go. We already had the method on floats and Decimals, and then it was added bool and float, so Fraction is the only one missing. I believe the goal was to get x.as_integer_ratio() to run without type testing on all concrete numeric types that could support it.Serhiy is proposing to instead add a math.as_integer_ratio() function that would extract the components differently for different types (seeissue 37822). If that had been our plan, then it was a mistake to add as_integer_ratio() to int and bool (as they already have numerator and denominator attributes). If you change you mind and want to go with 37822, we should probably rip-out the 3.8 addition to int and bool.Jeroen is proposing to down yet another path for this (seeissue 28716). He wants to add a __ratio__ method to all the classes and access them with an operator.ratio() function. He thinks this will help the SageMath package.My recommendation is to stick with the original plan of adding the as_integer_ratio() to all the concrete types. The only one left to be done is in the Fractions module. That is pretty easy -- seePR 15212.There is some question about whether to change numbers.Rational() but that discussion can be left for another day -- concrete classes are allowed to have extra methods that aren't in the abstract classes. The abstract classes are harder to change because any existing user classes that had registered as Rational would instantly be non-compliant (though the fix is easy).I would like to get this finished up for 3.8. It doesn't make sense to me to have as_integer_ratio() for bool, int, float, and Decimal but to have omitted the most obvious case, Fraction. That defeats the purpose of having parallel APIs. | |||
| msg349404 -(view) | Author: Guido van Rossum (gvanrossum)*![]() | Date: 2019-08-11 18:16 | |
Let's continue on the current path -- add Fraction.as_integer_ratio().Note that as_integer_ratio() is not part of the Numbers API, it is an optional protocol.You can count me out for Jeroen's __ratio__ proposal. | |||
| msg349411 -(view) | Author: Raymond Hettinger (rhettinger)*![]() | Date: 2019-08-11 21:41 | |
New changesetf03b4c8a48f62134799d368b78da35301af466a3 by Raymond Hettinger in branch 'master':bpo-37819: Add Fraction.as_integer_ratio() (GH-15212)https://github.com/python/cpython/commit/f03b4c8a48f62134799d368b78da35301af466a3 | |||
| msg349412 -(view) | Author: Raymond Hettinger (rhettinger)*![]() | Date: 2019-08-11 22:02 | |
New changeset5ba1cb03939bd86d026db667580f590966b573ea by Raymond Hettinger (Miss Islington (bot)) in branch '3.8':bpo-37819: Add Fraction.as_integer_ratio() (GH-15212) (GH-15215)https://github.com/python/cpython/commit/5ba1cb03939bd86d026db667580f590966b573ea | |||
| msg349413 -(view) | Author: Raymond Hettinger (rhettinger)*![]() | Date: 2019-08-11 22:03 | |
Thanks. | |||
| msg349936 -(view) | Author: Serhiy Storchaka (serhiy.storchaka)*![]() | Date: 2019-08-19 05:37 | |
Sorry, but I do not understand why adding Fraction.as_integer_ratio() prevents adding math.as_integer_ratio().The user code can not benefit from this until we add as_integer_ratio() to all numeric numbers, and this is not realistic. For the same reason there is str.join() which works with arbitrary iterable instead of adding the join() method to all collections, iterators and generators.math.as_integer_ratio() makes the user code more general, clear and fast. | |||
| msg349946 -(view) | Author: Jeroen Demeyer (jdemeyer)*![]() | Date: 2019-08-19 09:17 | |
> Sorry, but I do not understand why adding Fraction.as_integer_ratio() prevents adding math.as_integer_ratio().I also support a public function for that. It seems that we're planning this "as_integer_ratio" thing to become public API, so why not have a function as Serhiy proposes?I consider the situation with as_integer_ratio() very analogous to __index__ where we have operator.index(), so I would actually suggest operator.as_integer_ratio() but that's bikeshedding territory. | |||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:59:19 | admin | set | github: 82000 |
| 2019-08-19 09:17:27 | jdemeyer | set | nosy: +jdemeyer messages: +msg349946 |
| 2019-08-19 05:37:40 | serhiy.storchaka | set | messages: +msg349936 |
| 2019-08-11 22:03:01 | rhettinger | set | status: open -> closed resolution: fixed messages: +msg349413 stage: patch review -> resolved |
| 2019-08-11 22:02:28 | rhettinger | set | messages: +msg349412 |
| 2019-08-11 21:41:10 | miss-islington | set | pull_requests: +pull_request14943 |
| 2019-08-11 21:41:02 | rhettinger | set | messages: +msg349411 |
| 2019-08-11 18:16:52 | gvanrossum | set | messages: +msg349404 |
| 2019-08-11 18:00:16 | rhettinger | set | nosy: +gvanrossum messages: +msg349403 |
| 2019-08-11 17:38:02 | rhettinger | set | keywords: +patch stage: patch review pull_requests: +pull_request14940 |
| 2019-08-11 16:54:06 | rhettinger | set | messages: +msg349398 |
| 2019-08-11 16:41:19 | serhiy.storchaka | set | nosy: +serhiy.storchaka messages: +msg349397 |
| 2019-08-11 08:53:30 | scoder | set | nosy: +lukasz.langa,scoder messages: +msg349381 |
| 2019-08-11 07:17:44 | CuriousLearner | set | nosy: +CuriousLearner |
| 2019-08-11 07:01:53 | rhettinger | create | |