【问题标题】:Python 3.7 About PEP 484 multiple type how to explain it?Python 3.7 关于PEP 484 多类型怎么解释呢?
【发布时间】:2019-09-18 03:39:35
【问题描述】:
def read_image(file):
    image = imread(file)
    return image

在这种情况下,我接受文件是路径或图像数组,我如何对文件进行注释?

def read_image(file: str|array):

这可能吗?

更新

现在可以了。

关注@iggy12345

from typing import Union
def read_image(file: Union[str, list]):
    .....

【问题讨论】:

    标签: python-3.7 pep


    【解决方案1】:

    您需要做的是使用union 这允许你指定一个参数有多种类型,例如

    from typing import union
    
    def read_image(file: union[str, list]):
      pass
    

    有关使用联合的更多信息,请参阅here

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2016-10-26
      • 2019-03-21
      • 2016-08-03
      • 2018-03-01
      • 1970-01-01
      • 2019-10-07
      • 2018-11-05
      相关资源
      最近更新 更多