简体   繁体   中英

Python type hinting, output type depends on input type

Consider the following function

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

How do I hint that it returns

typing.List[type(el)]

That's what TypeVar is for:

from typing import TypeVar, List

T = TypeVar('T')

def make_list(el: T) -> List[T]:
    return [el, el]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM