Python type hinting, output type depends on input type

前端 未结 1 1845
夕颜
夕颜 2020-12-11 11:49

Consider the following function

import typing
def make_list(el : typing.Any):
    return [el, el]

相关标签:
1条回答
  • 2020-12-11 12:36

    That's what TypeVar is for:

    from typing import TypeVar, List
    
    T = TypeVar('T')
    
    def make_list(el: T) -> List[T]:
        return [el, el]
    
    0 讨论(0)
提交回复
热议问题