Python is DOOMED! Again!

Chris Kaynor ckaynor at zindagigames.com
Thu Jan 22 23:27:26 CET 2015


On Thu, Jan 22, 2015 at 2:12 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>>> def adder(a,b): return a+b
>>>
>>> This is one of the pythonic idioms that help with polymorphic functions.  Is
>>> there a proposal for providing hinting for these?
>>
>> You can use TypeVar for that.
>>
>> T = TypeVar('T')
>>
>> def adder(a: T, b: T) -> T:
>>     return a + b
>>
>> I'm not thrilled about having to actually declare T in this sort of
>> situation, but I don't have a better proposal.
>
> Hmm, but also that hinting doesn't cover cases like adder(12, 37.5)
> where two different types can be passed, and the return type could be
> either. I think for full generality you would just have to forgo
> hinting on this function.

Or use Any, which is basically the same thing:

def adder(a: Any, b: Any) -> Any:
    return a + b

Chris



More information about the Python-list mailing list