A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2015-October/141925.html below:

[Python-Dev] PEP 0484 - the Numeric Tower

[Python-Dev] PEP 0484 - the Numeric TowerLaura 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4