[Python-Dev] PEP 0484 - the Numeric Tower
Guido van Rossum
guido at python.org
Wed Oct 14 11:38:43 EDT 2015
More information about the Python-Dev mailing list
Wed Oct 14 11:38:43 EDT 2015
- Previous message (by thread): [Python-Dev] PEP 0484 - the Numeric Tower
- Next message (by thread): [Python-Dev] PEP 0484 - the Numeric Tower
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20151014/6f47b348/attachment.html>
- Previous message (by thread): [Python-Dev] PEP 0484 - the Numeric Tower
- Next message (by thread): [Python-Dev] PEP 0484 - the Numeric Tower
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-Dev mailing list