相关疑难解决方法(0)

装饰方法(类方法重载)

受Muhammad Alkarouri的启发,回答Python3的"功能注释"有什么用处,我想multimethod为方法做到这一点,而不是常规功能.但是,当我这样做

registry = {}

class MultiMethod(object):
    def __init__(self, name):
        self.name = name
        self.typemap = {}
    def __call__(self, *args):
        types = tuple(arg.__class__ for arg in args) # a generator expression!
        function = self.typemap.get(types)
    if function is None:
        raise TypeError("no match")
    return function(*args)
def register(self, types, function):
    if types in self.typemap:
        raise TypeError("duplicate registration")
    self.typemap[types] = function

def multimethod(function):
    name = function.__name__
    mm = registry.get(name)
    if mm is None:
        mm = registry[name] = MultiMethod(name)
    types = tuple(function.__annotations__.values()) …
Run Code Online (Sandbox Code Playgroud)

python methods decorator python-3.x

4
推荐指数
1
解决办法
1057
查看次数

标签 统计

decorator ×1

methods ×1

python ×1

python-3.x ×1