<div dir="ltr"><div class="gmail_extra">On Mon, Jan 14, 2013 at 11:38 PM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve+comp.lang.python@pearwood.info" target="_blank">steve+comp.lang.python@pearwood.info</a>></span> wrote:<br>

<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class=""><div class="h5">On Mon, 14 Jan 2013 23:00:16 -0500, Rodrick Brown wrote:<br>


<br>
> Can someone explain what's going on here.<br>
><br>
> def _build_magic_dispatcher(method):<br>
>     def inner(self, *args, **kwargs):<br>
>         return self.__dict__[method](*args, **kwargs)<br>
>     inner.__name__ = method<br>
>     return inner<br>
><br>
> Thanks.<br>
<br>
<br>
</div></div>This is a factory function, probably intended to be used as a decorator:<br>
<br>
class K:<br>
    @_build_magic_dispatcher<br>
    def something(self, x, y, z): ...<br>
<br>
except that it appears to be broken. It seems to be expecting a *string*,<br>
the name of a method, despite the function parameter claiming to require<br>
a method itself. So maybe you use it like this:<br>
<br>
class K:<br>
    def __init__(self):<br>
        self.parrot = _build_magic_dispatcher("parrot")<br>
<br>
<br>
or something similar. Without seeing the context, it's hard for me to<br>
tell whether it works or is broken. I suspect it is broken, or useless,<br>
or both.<br>
<br>
So, this factory function seems to take the *name* of a method as<br>
argument. Then it builds an inner method, which accepts arbitrary<br>
arguments (args and kwargs), renames the inner method to the name you<br>
passed as argument, and returns it.<br>
<br></blockquote><div><br></div><div style>Thanks Steven, here is the full context of the code. I'm trying to understand what exactly the author is trying to accomplish here. </div><div><br></div><div><div>import sys</div>

<div><br></div><div>PY3K = sys.version_info >= (3,)</div><div><br></div><div><br></div><div>methods = set([</div><div>    "__iter__",</div><div>    "__len__",</div><div>    "__contains__",</div>

<div><br></div><div>    "__lt__",</div><div>    "__le__",</div><div>    "__eq__",</div><div>    "__ne__",</div><div>    "__gt__",</div><div>    "__ge__",</div><div>

<br></div><div>    "__add__",</div><div>    "__and__",</div><div>    "__divmod__",</div><div>    "__floordiv__",</div><div>    "__lshift__",</div><div>    "__mod__",</div>

<div>    "__mul__",</div><div>    "__or__",</div><div>    "__pow__",</div><div>    "__rshift__",</div><div>    "__sub__",</div><div>    "__truediv__",</div><div>

    "__xor__",</div><div>])      </div><div>if PY3K:    </div><div>    methods.add("__next__")</div><div>    methods.add("__bool__")</div><div>else:       </div><div>    methods.add("__div__")</div>

<div>    methods.add("__nonzero__")</div><div>MAGIC_METHODS = frozenset(methods)</div><div>del methods</div></div><div><br></div><div><div>def _build_magic_dispatcher(method):</div><div>    def inner(self, *args, **kwargs):</div>

<div>        return self.__dict__[method](*args, **kwargs)</div><div>    inner.__name__ = method</div><div>    return inner</div><div><br></div><div><br></div><div>class stub(object):</div><div>    _classes_cache = {}</div>

<div><br></div><div>    def __new__(cls, **kwargs):</div><div>        magic_methods_present = MAGIC_METHODS.intersection(kwargs)</div><div>        if magic_methods_present not in cls._classes_cache:</div><div>            attrs = dict(</div>

<div>                (method, _build_magic_dispatcher(method))</div><div>                for method in magic_methods_present</div><div>            )</div><div>            attrs["__module__"] = cls.__module__</div>

<div>            cls._classes_cache[magic_methods_present] = type("stub", (cls,), attrs)</div><div>        new_cls = cls._classes_cache[magic_methods_present]</div><div>        return super(stub, new_cls).__new__(new_cls, **kwargs)</div>

<div><br></div><div>    def __init__(self, **kwargs):</div><div>        self.__dict__.update(kwargs)</div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">


The inner method simply looks up an attribute with the same name, and<br>
calls it as a function with whatever args and kwargs it gets.<br>
<br>
<br>
Does this help?<br>
<span class=""><font color="#888888"><br>
<br>
<br>
--<br>
Steven<br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br></div></div>