我有一种数据类型,它只是对相关数据进行分组。它应该是一个类似于struct的东西,所以我选择了namedtuple
。你知道吗
ConfigOption = namedtuple('ConfigOption', 'one two animal vehicle fairytale')
另一方面,namedtuple
没有默认值,因此我使用了another answer中提出的一个hack。你知道吗
ConfigOption.__new__.__defaults__ = (1, 2, "White Horse", "Pumpkin", "Cinderella")
显然,这使得类型检查失败:error: "Callable[[Type[NT], Any, Any, Any, Any, Any], NT]" has no attribute "__defaults__"
因为我很清楚这是一个黑客,所以我告诉类型检查器使用内联注释# type: disable
:
ConfigOption.__new__.__defaults__ = (1, 2, "White Horse", "Pumpkin", "Cinderella") # type: disable
在这个时候。。。这条线太长了。我不知道如何打破这一行,使其语法正确,同时使类型检查器跳过它:
# the ignore is on the wrong line
ConfigOption.__new__.__defaults__ = \
(1, 2, "White Horse", "Pumpkin", "Cinderella") # type: ignore
# unexpected indentation
ConfigOption.__new__.__defaults__ = # type: ignore
(1, 2, "White Horse", "Pumpkin", "Cinderella")
那么,有没有办法从类型检查中排除一行,或者格式化这一长行,以便跳过类型检查,并且行长度符合pep-8呢?你知道吗
有什么问题吗:
Enum似乎遵循了您需要的约束,而且非常简洁。你知道吗
您可以使用Functional API,它本身表示
semantics resemble namedtuple
相关问题 更多 >
编程相关推荐