初始化 TypedDict 并稍后填充键和值
Initialize a TypedDict and fill keys & values later
我有一个字典,其中的键和值的类型是固定的。我想在 TypedDict
中定义类型如下:
class MyTable(TypedDict):
caption: List[str]
header: List[str]
table: pd.DataFrame
epilogue: List[str]
我有 returns 一个 MyTable
的功能。我想先定义一个空的 (Typed)dict
并填写键和值。
def returnsMyTable():
result = {}
result['caption'] = ['caption line 1','caption line 2']
result['header'] = ['header line 1','header line 2']
result['table'] = pd.DataFrame()
result['epilogue'] = ['epilogue line 1','epilogue line 2']
return result
这里 MyPy 抱怨需要结果的类型注释。我试过这个:
result: MyTable = {}
但随后 MyPy 抱怨密钥丢失。同样,如果我定义键但将值设置为 None
,它会抱怨值的类型不正确。
是否可以先将 TypedDict
初始化为空 Dict,然后再填充键和值? docs 似乎暗示它是。
我想我可以先将值定义为变量,然后再将 assemble 定义为 MyTable
,但我正在处理要添加类型提示的遗留代码。所以我想尽量减少工作。
你可能想要在这里设置整体性,但我会三思而后行。
引用 the PEP
By default, all keys must be present in a TypedDict. It is possible to override this by specifying totality. Here is how to do this using the class-based syntax:
class MyTable(TypedDict, total=False):
caption: List[str]
header: List[str]
table: pd.DataFrame
epilogue: List[str]
result: MyTable = {}
result2: MyTable = {"caption": ["One", "Two", "Three"]}
正如我所说,请三思而后行。总计 TypedDict
可以很好地保证所有项目都存在。也就是说,因为 MyPy 不允许没有“标题”的结果存在,您可以安全地调用 cap = result["caption"]
.
如果您设置 total=False,该保证就会消失。假设您 MyTable
的使用频率远高于制造频率,那么在使用时获得额外的安全保证可能是一笔不错的交易。
就我个人而言,我会保留 total=False
用于创建代码有时确实遗漏了一些东西并且使用它的任何代码都必须处理的情况。如果只是需要几行来初始化,我会这样做:
def returnsMyTable():
result = {}
result_caption = ['caption line 1','caption line 2']
result_header = ['header line 1','header line 2']
result_table = pd.DataFrame()
result_epilogue = ['epilogue line 1','epilogue line 2']
result = {
"caption": result_caption,
"header": result_header,
"table": result_table,
"epilogue": result_epilogue
}
return result
我有一个字典,其中的键和值的类型是固定的。我想在 TypedDict
中定义类型如下:
class MyTable(TypedDict):
caption: List[str]
header: List[str]
table: pd.DataFrame
epilogue: List[str]
我有 returns 一个 MyTable
的功能。我想先定义一个空的 (Typed)dict
并填写键和值。
def returnsMyTable():
result = {}
result['caption'] = ['caption line 1','caption line 2']
result['header'] = ['header line 1','header line 2']
result['table'] = pd.DataFrame()
result['epilogue'] = ['epilogue line 1','epilogue line 2']
return result
这里 MyPy 抱怨需要结果的类型注释。我试过这个:
result: MyTable = {}
但随后 MyPy 抱怨密钥丢失。同样,如果我定义键但将值设置为 None
,它会抱怨值的类型不正确。
是否可以先将 TypedDict
初始化为空 Dict,然后再填充键和值? docs 似乎暗示它是。
我想我可以先将值定义为变量,然后再将 assemble 定义为 MyTable
,但我正在处理要添加类型提示的遗留代码。所以我想尽量减少工作。
你可能想要在这里设置整体性,但我会三思而后行。
引用 the PEP
By default, all keys must be present in a TypedDict. It is possible to override this by specifying totality. Here is how to do this using the class-based syntax:
class MyTable(TypedDict, total=False):
caption: List[str]
header: List[str]
table: pd.DataFrame
epilogue: List[str]
result: MyTable = {}
result2: MyTable = {"caption": ["One", "Two", "Three"]}
正如我所说,请三思而后行。总计 TypedDict
可以很好地保证所有项目都存在。也就是说,因为 MyPy 不允许没有“标题”的结果存在,您可以安全地调用 cap = result["caption"]
.
如果您设置 total=False,该保证就会消失。假设您 MyTable
的使用频率远高于制造频率,那么在使用时获得额外的安全保证可能是一笔不错的交易。
就我个人而言,我会保留 total=False
用于创建代码有时确实遗漏了一些东西并且使用它的任何代码都必须处理的情况。如果只是需要几行来初始化,我会这样做:
def returnsMyTable():
result = {}
result_caption = ['caption line 1','caption line 2']
result_header = ['header line 1','header line 2']
result_table = pd.DataFrame()
result_epilogue = ['epilogue line 1','epilogue line 2']
result = {
"caption": result_caption,
"header": result_header,
"table": result_table,
"epilogue": result_epilogue
}
return result