<div dir="ltr">Oh, I probably want `return fn` inside my inner decorator. Otherwise, the defined name gets bound to None in the global scope. I'm not sure, maybe that's better... but most likely we should leave the name for other users. I just wrote it without testing.<div class="gmail_extra"><br><div class="gmail_quote">On Sun, Feb 12, 2017 at 10:19 AM, David Mertz <span dir="ltr"><<a href="mailto:mertz@gnosis.cx" target="_blank">mertz@gnosis.cx</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>But we already *have* decorators! Here's a nice factory for them:</div><div><br></div></div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div><font face="monospace, monospace">def attach_to(thing, name=None):</font></div></div></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div><font face="monospace, monospace"> def decorator(fn):</font></div></div></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div><font face="monospace, monospace"> if name is None:</font></div></div></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div><font face="monospace, monospace"> name = fn.__name__</font></div></div></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div><font face="monospace, monospace"> setattr(thing, name, fn)</font></div></div></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div class="gmail_quote"><div><font face="monospace, monospace"> return decorator</font></div></div></div></blockquote><div class="gmail_extra"><div class="gmail_quote"><div><span style="font-family:monospace,monospace"> </span><br></div></div>This does everything you are asking for, e.g.:</div><div class="gmail_extra"><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><font face="monospace, monospace">my_menu = Menu()</font></div><div class="gmail_extra"><font face="monospace, monospace"><br></font></div><div class="gmail_extra"><font face="monospace, monospace">@attach_to(my_menu)</font></div><div class="gmail_extra"><font face="monospace, monospace">def callback(self, ...)</font></div><div class="gmail_extra"><font face="monospace, monospace"> ...</font></div></blockquote><div class="gmail_extra"><div><br></div><div>I got extra fancy with two lines to allow you to either use the same name as the function itself or pick a custom name for the attribute.</div></div></div></blockquote></div><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons. Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br></div>
</div></div>