我有一个与defaultdictdict类似的自定义子类,但将缺少的键传递给它,以便它可以生成适当的值。default_factory
class KeyDefaultDict(dict):
__slots__ = ("default_factory",)
def __init__(self, default_factory, *args, **kwargs):
super().__init__(*args, **kwargs)
self.default_factory = default_factory
def __missing__(self, key):
if self.default_factory is None:
raise KeyError(key)
ret = self[key] = self.default_factory(key)
return ret
def __repr__(self):
return (
f"{type(self).__name__}({repr(self.default_factory)}, {super().__repr__()})"
)
d = KeyDefaultDict(int)
print(d["1"] + d["2"] + d["3"]) # out: 6
print(d) # out: KeyDefaultDict(<class 'int'>, {'1': 1, '2': 2, '3': 3})
Run Code Online (Sandbox Code Playgroud)
我想像项目的其余部分一样为此类添加类型注释,但我找不到任何有关如何执行此操作的示例。我看到该typing模块使用外部类来添加注释。例如,defaultdict将用typing.DefaultDict其定义为 进行注释class typing.DefaultDict(collections.defaultdict, MutableMapping[KT, VT])。
所以它是一个外部类,它是defaultdict泛型的子类typing.MutableMapping。不过,我想他们这样做可能是因为他们不想改变原来的collections.defaultdict。Generic我找到了和 的子类的示例Mapping,但没有找到从其他东西继承的类,例如dict.
问题是:如何为此类添加类型注释以使其成为泛型类?我是否需要扩展其他东西或为注释创建一个外部类?
我使用的是 python 3.7.5,我更喜欢直接继承dict,因此我不必实现所需的方法并且出于性能原因。
提前致谢。
我很晚了,但我刚刚在我自己的代码库中得到了这个工作。
基本上,您需要使用类型映射泛型
这是 dict 使用的泛型,因此您可以定义其他类型,例如MyDict[str, int].
对于我的用例,我想要一个特殊的字典,它可以干净地格式化自己以进行日志记录,但我将在各种类型中使用它,因此它需要输入支持。
如何:
import typing
# these are generic type vars to tell mapping to accept any type vars when creating a type
_KT = typing.TypeVar("_KT") # key type
_VT = typing.TypeVar("_VT") # value type
# `typing.Mapping` requires you to implement certain functions like __getitem__
# I didn't want to do that, so I just subclassed dict.
# Note: The type you're subclassing needs to come BEFORE
# the `typing` subclass or the dict won't work.
# I had a test fail where my debugger showed that the dict had the items,
# but it wouldn't actually allow access to them
class MyDict(dict, typing.Mapping[_KT, _VT]):
"""My custom dict that logs a special way"""
def __str__(self):
# This function isn't necessary for your use-case, just including as example code
return clean_string_helper_func(
super(MyDict, self).__str__()
)
# Now define the key, value typings of your subclassed dict
RequestDict = MyDict[str, typing.Tuple[str, str]]
ModelDict = MyDict[str, typing.Any]
Run Code Online (Sandbox Code Playgroud)
现在使用子类字典的自定义类型:
from my_project.custom_typing import RequestDict # Import your custom type
request = RequestDict()
request["test"] = ("sierra", "117")
print(request)
Run Code Online (Sandbox Code Playgroud)
将输出为{ "test": ("sierra", "117") }
| 归档时间: |
|
| 查看次数: |
2255 次 |
| 最近记录: |