【问题标题】:Python validate arg is a list of specific objectPython validate arg 是特定对象的列表
【发布时间】:2019-08-03 16:42:01
【问题描述】:

我创建了一个类 -

class Class1,而在其他模块中,我希望有一个函数,该函数将Class1 列表作为参数获取(并确保它是该对象的列表)。我想要类似:def my_function(class1: list<Class1>):。但是,这似乎不起作用(interperter 不会接受这个)。我该怎么做?

【问题讨论】:

    标签: python list class types


    【解决方案1】:

    正确的语法是使用方括号

    from typing import Iterable, List
    
    # Limited to just lists
    def my_function(class1: List[Class1]):
        pass
    
    # Will accept any type of iterable
    def my_function(class1: Iterable[Class1]):
        pass
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      • 2012-01-03
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多