我在这里阅读了一篇关于命名元组类型提示的文章,但是它只涉及到命名元组。
是否有可能为包含namedtuple
的List
创建类型提示?
例如:
firefoxprofile = namedtuple("Profile", ["Name", "Path", "isRelative", "Default"])
# Will contain a list of tuples that represent the firefox profiles.
ffprofiles = [] # -- how would I write the type hint?
ffprofiles.append(Profile(Name='Jason', Path='Profiles/er5rtak4.Jason', isRelative='1', Default=None))
ffprofiles.append(Profile(Name='Sarah', Path='Profiles/23mvfqcj.Sarah', isRelative='1', Default=None))
我尝试过:
ffprofiles = List[namedtuple("Profile", ["Name", "Path", "isRelative", "Default"])]
但是当我尝试使用该语法更新ffprofiles = []
行时,它不起作用,并出现异常:
TypeError: descriptor 'append' requires a 'list' object but received a 'Profile'
List[firefoxprofile]
? - Aran-FeyList[firefoxprofile]
。对我来说可以工作。 - timgebList
?也许我没有正确地声明List
。 - user9814172List[firefoxprofile]
->typing.List[__main__.Profile]
。你的错误在其他地方。 - timgebList
,即ffprofiles = []
时,它可以工作。 - user9814172