Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python-在修饰类多方法时丢失了自我_Python_Multidispatch - Fatal编程技术网

python-在修饰类多方法时丢失了自我

python-在修饰类多方法时丢失了自我,python,multidispatch,Python,Multidispatch,我正试图基于本文实现一种多方法方法。这种方法有两个不同之处: 我只需要查看multimethod的第一个参数,因此不需要形成arg类的元组 多重方法将存在于类中,它们将不是正则函数 然而,我把我的类弄混了一点,对self的调用在调用类方法时丢失了 这是我的密码: method_registry = {} class SendMessageMultiMethod(object): """ A class for implementing multimethod functi

我正试图基于本文实现一种多方法方法。这种方法有两个不同之处:

  • 我只需要查看multimethod的第一个参数,因此不需要形成arg类的元组
  • 多重方法将存在于类中,它们将不是正则函数
然而,我把我的类弄混了一点,对
self
的调用在调用类方法时丢失了

这是我的密码:

method_registry = {}


class SendMessageMultiMethod(object):
    """
    A class for implementing multimethod functionality
    """
    def __init__(self, name):
        self.name = name
        self.typemap = {}

    def __call__(self, message, extra_payload=None):
        """
        Overrriding method call and dispatching it to an actual method
        based on the supplied message class
        """
        first_arg_type = message.__class__
        function = self.typemap.get(first_arg_type)
        print(
            'Dispatching to function {} with message {} and extra payload {}...'
            .format(function, message, extra_payload)
        )
        return function(message, extra_payload)

    def register(self, type_, function):
        self.typemap[type_] = function


def use_for_type(*types):
    """
    A decorator that registers a method to use with certain types
    """

    def register(method):
        """Creating Multimethod with the method name
        and registering it at at method_registry dict """
        name = method.__name__
        mm = method_registry.get(name)
        if mm is None:
            mm = method_registry[name] = SendMessageMultiMethod(name)
        for type_ in types:
            mm.register(type_, method)
        return mm

    return register


class Sender(object):

    def send_messages(self, messages_list):
        for message in messages_list:
            # this is supposed to fire different send_message() methods
            # for different arg types
            self.send_message(message)

    @use_for_type(int, float)
    def send_message(self, message, *args, **kwargs):
        print('received call for int/float message {} with {}, {}'
              .format(message, args, kwargs))
        print('self is {}'.format(self))

    @use_for_type(bool)
    def send_message(self, message, *args, **kwargs):
        print('received call for bool message {} with {}, {}'
              .format(message, args, kwargs))
        print('self is {}'.format(self))
因此,当我在
Sender
类上调用
send\u messages
方法时,我接收
self
中的参数,而不是
message
变量中的参数。在这里:

sender = Sender()
sender.send_messages([1, 2, True, 5.6])
输出:

Dispatching to function <function Sender.send_message at 0x1013608c8> with message 1 and extra payload None...
received call for int/float message None with (), {}
self is 1
Dispatching to function <function Sender.send_message at 0x1013608c8> with message 2 and extra payload None...
received call for int/float message None with (), {}
self is 2
Dispatching to function <function Sender.send_message at 0x101360950> with message True and extra payload None...
received call for bool message None with (), {}
self is True
Dispatching to function <function Sender.send_message at 0x1013608c8> with message 5.6 and extra payload None...
received call for int/float message None with (), {}
self is 5.6
调度到具有消息1和额外有效负载无的功能。。。
接收到对int/float消息None的调用,带有(),{}
赛尔夫是1
正在调度到具有消息2和额外有效负载无的功能。。。
接收到对int/float消息None的调用,带有(),{}
赛尔夫2岁
正在调度到具有消息True和额外有效负载None的函数。。。
已收到对bool消息None的调用,其中包含(),{}
自我是真实的
发送到带有消息5.6和额外有效负载的功能无。。。
接收到对int/float消息None的调用,带有(),{}
赛尔夫是5.6

如何不丢失
self
并将消息内容分派到
message
变量?

正如
def send\u message(self,message,*args,**kwargs)等Python方法签名所建议的那样,方法的第一个参数必须是
self
对象。通常,通过执行
obj.send_message
,您可以访问对象的方法,而不是类。请尝试以下操作:

>>> class Foo():
...     def bar(self):
...         pass

>>> Foo.bar
<function __main__.Foo.bar>

>>> Foo().bar
<bound method Foo.bar of <__main__.Foo object at 0x7f5fd927cc18>>
输出:

Dispatching to function <function Sender.send_message at 0x7f1e427e5488> with message 1 and extra payload None...
received call for int/float message 1 with (None,), {}
self is <__main__.Sender object at 0x7f1e4277b0f0>
Dispatching to function <function Sender.send_message at 0x7f1e427e5488> with message 2 and extra payload None...
received call for int/float message 2 with (None,), {}
self is <__main__.Sender object at 0x7f1e4277b0f0>
Dispatching to function <function Sender.send_message at 0x7f1e427e5598> with message True and extra payload None...
received call for bool message True with (None,), {}
self is <__main__.Sender object at 0x7f1e4277b0f0>
Dispatching to function <function Sender.send_message at 0x7f1e427e5488> with message 5.6 and extra payload None...
received call for int/float message 5.6 with (None,), {}
self is <__main__.Sender object at 0x7f1e4277b0f0>
调度到具有消息1和额外有效负载无的功能。。。
接收到对int/float消息1的调用,其中包含(无、)、{}
自我是
正在调度到具有消息2和额外有效负载无的功能。。。
接收到对int/float消息2的调用,其中包含(无、)、{}
自我是
正在调度到具有消息True和额外有效负载None的函数。。。
接收到对bool消息True的调用,带有(无,),{}
自我是
发送到带有消息5.6和额外有效负载的功能无。。。
接收到对int/float消息5.6的调用,其中包含(无、)、{}
自我是

除了我的答案之外,我建议您在这里查看:,特别是
核心
调度程序
,了解更完整实现的代码和/或灵感。
Dispatching to function <function Sender.send_message at 0x7f1e427e5488> with message 1 and extra payload None...
received call for int/float message 1 with (None,), {}
self is <__main__.Sender object at 0x7f1e4277b0f0>
Dispatching to function <function Sender.send_message at 0x7f1e427e5488> with message 2 and extra payload None...
received call for int/float message 2 with (None,), {}
self is <__main__.Sender object at 0x7f1e4277b0f0>
Dispatching to function <function Sender.send_message at 0x7f1e427e5598> with message True and extra payload None...
received call for bool message True with (None,), {}
self is <__main__.Sender object at 0x7f1e4277b0f0>
Dispatching to function <function Sender.send_message at 0x7f1e427e5488> with message 5.6 and extra payload None...
received call for int/float message 5.6 with (None,), {}
self is <__main__.Sender object at 0x7f1e4277b0f0>