[Python-Dev] PEP 0484 - the Numeric Tower (original) (raw)

Laura Creighton lac at openend.se
Wed Oct 14 16:56:22 EDT 2015


In a message of Wed, 14 Oct 2015 08:38:43 -0700, Guido van Rossum writes:

Perhaps you could solve this with type variables. Here's a little demonstration program: _ _from decimal import Decimal_ _from typing import TypeVar_ _F = TypeVar('F', float, Decimal)_ _def add(a: F, b: F) -> F:_ _return a+b_ _print(add(4.2, 3.14))_ _print(add(Decimal('4.2'), Decimal('3.14')))_ _print(add(Decimal('4.2'), 3.14))_ _ Note that the last line is invalid. mypy correctly finds this: _ _flt.py:8: error: Type argument 1 of "add" has incompatible value "object"_ _ (We could work on the error message.)

Now, I'm not sure that this is the best solution given the audience -- but the type variable 'F' only needs to be defined once, so this might actually work. -- --Guido van Rossum (python.org/~guido)

This looks good to me. I wonder if there is anything we can do, documentation and PEP wise to encourage people who write code to use it, rather than just using float?

Laura



More information about the Python-Dev mailing list