from collections import defaultdict
class ExtensionPoint:
def __init__(self):
self.__items = []
def register(self, module, item):
self.__items.append((module, item))
def unregister_module(self, name):
self.__items = [item for item in self.__items if item[1] != name]
priority = 1
class PluginFunctions:
def __init__(self):
self.functions = defaultdict(ExtensionPoint)
def register(self, module, item):
self.functions[priority].register(module, item)
def unregister(self, item):
self.functions[priority].unregister_module(item)
def helper(self):
print(self.functions)
obj = PluginFunctions()
obj. register("a","b")
obj.helper()
obj.unregister("b")
obj.helper()
为什么这两个辅助函数都返回相同的值?如何使取消注册功能实际上从dict中删除值?我没有得到这个代码的错误。