【问题标题】:Is there a way to combine more than one type for typing [duplicate]有没有一种方法可以组合多种类型来输入[重复]
【发布时间】:2021-02-21 05:32:12
【问题描述】:

使用类型提示库typing 是否可以组合两种类型?我想为参数设置一个 strlist of str。见下文:

from typing import Dict, Optional, List


def run_ml(
        estimator: Estimator,
        parameters: Optional[Dict[str, List[str]]] = None,
):

【问题讨论】:

    标签: python python-3.x type-hinting


    【解决方案1】:

    是的。你可以使用Union

    例子

    def send_email(address: Union[str, List[str]],
                   sender: str,
                   cc: Optional[List[str]],
                   bcc: Optional[List[str]],
                   subject='',
                   body: Optional[List[str]] = None
                   ) -> bool:
    

    【讨论】:

      【解决方案2】:

      您可以使用typing.Union 像这样Union[List[str], str]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 2011-12-22
        • 1970-01-01
        • 2020-01-21
        • 2020-08-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多