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 _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com