Raymond Hettinger wrote:
FYI, I had already updated the patch to incorporate your suggestion. It checks __index__ first and then __float__ if index doesn't exist. The result makes it work with Decimal inputs.
Only sort of - the domain of the output is then constrained by what the builtin float type can represent, which strikes me as fairly inappropriate for what should be a lossless display operation (both fractions.Fraction and decimal.Decimal can represent a lot of numbers that float can't deal with properly). To use the old faithful example of this, given:
what happens for bin(Decimal("1.1")) with the patch version that uses __float__?
As I've said elsewhere in this thread, I'm certainly not averse to the basic idea of letting bin/oct/hex handle rational numbers as well as integers, I just don't see it as something we have to have in the current release cycle, and nor do I see it really working properly without adding a __rational__ special method that returns a numerator/denominator 2-tuple (which would obviously require a PEP).