This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Fraction.from_any()
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger
Priority: normal Keywords: patch

Created on 2008-07-04 19:36 by rhettinger, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
from_any.diff rhettinger, 2008-07-04 19:36 Patch to fractions.py
Messages (2)
msg69264 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-07-04 19:36
After exercising the fractions module, I've found it problematic that 
there isn't a unified constructor to handle mixed data input types.  

For example, when updating the accurate summation recipe at 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/393090 to show 
how it could be done with rational arithmetic.  Handling mixed-type 
inputs required writing a factory function:

    def makefrac(x):
        if isinstance(x, Decimal):
            return Fraction.from_decimal(x)
        if isinstance(x, float):
            return Fraction.from_float(x)
        return Fraction(x)

That function supported a clean version of the recipe:

    def frsum(iterable):
        return float(sum(map(makefrac, iterable)))

It would have been much better if that functionality were built into 
the class definition.  See attached patch.
msg69505 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-07-10 14:36
Adopted the solution used by functions in the math module.  Functions 
that accept floats also accept integral inputs.  This improves 
usability in face of mixed float/int data or decimal/int data.

See r64846.
History
Date User Action Args
2022-04-11 14:56:36adminsetgithub: 47535
2008-07-10 14:36:31rhettingersetstatus: open -> closed
resolution: fixed
messages: + msg69505
2008-07-04 19:36:26rhettingercreate