(cls, numerator=0, denominator=None, *, _normalize=True)
return self
Constructs a Rational.

        Takes a string like '3/2' or '1.5', another Rational instance, a
        numerator/denominator pair, or a float.

        Examples
        --------

        >>> Fraction(10, -8)
        Fraction(-5, 4)
        >>> Fraction(Fraction(1, 7), 5)
        Fraction(1, 35)
        >>> Fraction(Fraction(1, 7), Fraction(2, 3))
        Fraction(3, 14)
        >>> Fraction('314')
        Fraction(314, 1)
        >>> Fraction('-35/4')
        Fraction(-35, 4)
        >>> Fraction('3.1415') # conversion from numeric string
        Fraction(6283, 2000)
        >>> Fraction('-47e-2') # string may include a decimal exponent
        Fraction(-47, 100)
        >>> Fraction(1.47)  # direct construction from float (exact conversion)
        Fraction(6620291452234629, 4503599627370496)
        >>> Fraction(2.25)
        Fraction(9, 4)
        >>> Fraction(Decimal('1.47'))
        Fraction(147, 100)
Constructs a Rational.
Constructs a Rational .
def __new__(cls, numerator=0, denominator=None, *, _normalize=True):
        """Constructs a Rational.

        Takes a string like '3/2' or '1.5', another Rational instance, a
        numerator/denominator pair, or a float.

        Examples
        --------

        >>> Fraction(10, -8)
        Fraction(-5, 4)
        >>> Fraction(Fraction(1, 7), 5)
        Fraction(1, 35)
        >>> Fraction(Fraction(1, 7), Fraction(2, 3))
        Fraction(3, 14)
        >>> Fraction('314')
        Fraction(314, 1)
        >>> Fraction('-35/4')
        Fraction(-35, 4)
        >>> Fraction('3.1415') # conversion from numeric string
        Fraction(6283, 2000)
        >>> Fraction('-47e-2') # string may include a decimal exponent
        Fraction(-47, 100)
        >>> Fraction(1.47)  # direct construction from float (exact conversion)
        Fraction(6620291452234629, 4503599627370496)
        >>> Fraction(2.25)
        Fraction(9, 4)
        >>> Fraction(Decimal('1.47'))
        Fraction(147, 100)

        """
        self = super(Fraction, cls).__new__(cls)

        if denominator is None:
            if type(numerator) is int:
                self._numerator = numerator
                self._denominator = 1
                return self

            elif isinstance(numerator, numbers.Rational):
                self._numerator = numerator.numerator
                self._denominator = numerator.denominator
                return self

            elif isinstance(numerator, (float, Decimal)):
                # Exact conversion
                self._numerator, self._denominator = numerator.as_integer_ratio()
                return self

            elif isinstance(numerator, str):
                # Handle construction from strings.
                m = _RATIONAL_FORMAT.match(numerator)
                if m is None:
                    raise ValueError('Invalid literal for Fraction: %r' %
                                     numerator)
                numerator = int(m.group('num') or '0')
                denom = m.group('denom')
                if denom:
                    denominator = int(denom)
                else:
                    denominator = 1
                    decimal = m.group('decimal')
                    if decimal:
                        scale = 10**len(decimal)
                        numerator = numerator * scale + int(decimal)
                        denominator *= scale
                    exp = m.group('exp')
                    if exp:
                        exp = int(exp)
                        if exp >= 0:
                            numerator *= 10**exp
                        else:
                            denominator *= 10**-exp
                if m.group('sign') == '-':
                    numerator = -numerator

            else:
                raise TypeError("argument should be a string "
                                "or a Rational instance")

        elif type(numerator) is int is type(denominator):
            pass # *very* normal case

        elif (isinstance(numerator, numbers.Rational) and
            isinstance(denominator, numbers.Rational)):
            numerator, denominator = (
                numerator.numerator * denominator.denominator,
                denominator.numerator * numerator.denominator
                )
        else:
            raise TypeError("both arguments should be "
                            "Rational instances")

        if denominator == 0:
            raise ZeroDivisionError('Fraction(%s, 0)' % numerator)
        if _normalize:
            g = math.gcd(numerator, denominator)
            if denominator < 0:
                g = -g
            numerator //= g
            denominator //= g
        self._numerator = numerator
        self._denominator = denominator
        return self
def __new__ ( cls , numerator = 0 , denominator = None , * , _normalize = True ) : self = super ( Fraction , cls ) . __new__ ( cls ) if denominator is None : if type ( numerator ) is int : self . _numerator = numerator self . _denominator = 1 return self elif isinstance ( numerator , numbers . Rational ) : self . _numerator = numerator . numerator self . _denominator = numerator . denominator return self elif isinstance ( numerator , ( float , Decimal ) ) : # Exact conversion self . _numerator , self . _denominator = numerator . as_integer_ratio ( ) return self elif isinstance ( numerator , str ) : # Handle construction from strings. m = _RATIONAL_FORMAT . match ( numerator ) if m is None : raise ValueError ( 'Invalid literal for Fraction: %r' % numerator ) numerator = int ( m . group ( 'num' ) or '0' ) denom = m . group ( 'denom' ) if denom : denominator = int ( denom ) else : denominator = 1 decimal = m . group ( 'decimal' ) if decimal : scale = 10 ** len ( decimal ) numerator = numerator * scale + int ( decimal ) denominator *= scale exp = m . group ( 'exp' ) if exp : exp = int ( exp ) if exp >= 0 : numerator *= 10 ** exp else : denominator *= 10 ** - exp if m . group ( 'sign' ) == '-' : numerator = - numerator else : raise TypeError ( "argument should be a string " "or a Rational instance" ) elif type ( numerator ) is int is type ( denominator ) : pass # *very* normal case elif ( isinstance ( numerator , numbers . Rational ) and isinstance ( denominator , numbers . Rational ) ) : numerator , denominator = ( numerator . numerator * denominator . denominator , denominator . numerator * numerator . denominator ) else : raise TypeError ( "both arguments should be " "Rational instances" ) if denominator == 0 : raise ZeroDivisionError ( 'Fraction(%s, 0)' % numerator ) if _normalize : g = math . gcd ( numerator , denominator ) if denominator < 0 : g = - g numerator //= g denominator //= g self . _numerator = numerator self . _denominator = denominator return self