• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

python 搞代码 3年前 (2022-02-20) 17次浏览 已收录 0个评论

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 😀


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于python:Writing-Functions-with-Same-Name-and-Different-Parameters
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址