关于python:Writing-Functions-with-Same-Name-and-Different-Parameters

54次阅读

共计 2115 个字符,预计需要花费 6 分钟才能阅读完成。

I’ve been wondering how to write functions and methods with the same name but different parameters, as if we had “overloaded” them. According to wiki, this is called multiple dispatching because in Python it happens in run time but not compile time.

Fortunately, there has been a discussion on Stack Overflow: python-function-overload. People have showed various ways to solve this problem. For example, here’s a third-party library we can use: https://github.com/coady/mult…

I’ve also come up with a solution to this problem by slightly changing the code of David Beazley, the author of Python Cookbook, who used function annotations to implement multiple dispatchings. He has kindly put his code here: https://github.com/dabeaz/pyt…

And here’s my code. The main idea is to use a dictionary to map the name of arguments (instead of the annotation) to the corresponding functions. I stored the arguments’ names in a frozen set so that argument list [a, b, c] and [c, b, a] mean to call the same function.

class MultiMethod:
    
    def __init__(self, name):
        self._methods = {}
        self.__name__ = name
    
    def register(self, meth):
        sig = inspect.signature(meth)
        arg_names = []
        for name, parm in sig.parameters.items():
            if name == 'self':
                continue
            if parm.default is not inspect.Parameter.empty:
                self._methods[frozenset(arg_names)] = meth  
            
            # we can make var_names a frozen set 
            # so that f(a, b, c) and f(c, b, a) means the same thing
            arg_names.append(name)
            
        self._methods[frozenset(arg_names)] = meth
    
    def __call__(self, *args, **kwargs):
        """
        call a method based on name of the arguments
        *args is used to hold the "self" argument of the caller, which is 
        an instance of a class 
        """
        arg_names = frozenset(kwargs.keys())
        meth = self._methods.get(arg_names, None)
        if meth:
            return meth(*args, **kwargs)
        else:
            raise TypeError(f"No matching method for arguments {arg_names}")
    def __get__(self, instance, cls):
        if instance is not None:
        # MethodType bounds a method to an instance
            return types.MethodType(self, instance)  
        else:
            return self

This is enough to solve the problem raised in the discussion:

And we can use the rest of the code proivded by David to create a class whose methods can be overloaded:

All that said, in Python we can just use functions with different names to handle most of the situations. AoP also suggests us use overloading only when the functions perform the same task but have arguments of different types. So all of these multiple dispatching stuff I write here is just for summary, practice and fun :D

正文完
 0