[New-bugs-announce] [issue3285] Fraction.from_any()

Raymond Hettinger report at bugs.python.org
Fri Jul 4 21:36:27 CEST 2008


New submission from Raymond Hettinger <rhettinger at users.sourceforge.net>:

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.

----------
components: Library (Lib)
files: from_any.diff
keywords: patch
messages: 69264
nosy: rhettinger
severity: normal
status: open
title: Fraction.from_any()
type: feature request
versions: Python 2.6, Python 3.0
Added file: http://bugs.python.org/file10810/from_any.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3285>
_______________________________________


More information about the New-bugs-announce mailing list