<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><span class=""><div dir="auto"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="m_-8698139113957381197quote" 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">Attaching to the instance is fine too. But I prefer the current spelling so far:<br></div></div></div></blockquote></div></div></div></span><span class=""><div dir="auto"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="m_-8698139113957381197quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div class="gmail_extra"><div class="gmail_quote"><div><br></div></div></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div class="gmail_extra"><div class="gmail_quote"><div><div>my_menu1 = Menu(['Pizza', 'Cake', 'Pasta'])</div></div></div></div></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div class="gmail_extra"><div class="gmail_quote"><div>my_menu2 = Menu(...)</div></div></div></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div class="gmail_extra"><div class="gmail_quote"><div><br></div></div></div></div><div><div class="gmail_extra"><div class="gmail_quote"><div><div>def callback1(self, ...):</div></div></div></div></div><div><div class="gmail_extra"><div class="gmail_quote"><div><div> ...</div></div><div><div><div class="gmail_extra"><div class="gmail_quote">def callback2(self, ...):</div></div></div><div><div class="gmail_extra"><div class="gmail_quote"> ...</div><div class="gmail_quote"><br></div></div></div><div><div class="gmail_extra"><div class="gmail_quote"><div></div></div></div></div></div></div></div></div><div><div class="gmail_extra"><div class="gmail_quote"><div><div>my_menu1.callback = callback2</div></div></div></div></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div class="gmail_extra"><div class="gmail_quote"><div>my_menu2.callback = callback1</div></div></div></div></blockquote></div></blockquote></div></div></div><div dir="auto"><br></div></span><div dir="auto">I don't, it is repeating the variable name three times. I don't see how this differs from decorator syntax, do you prefer the old way on that too, or am I missing something?</div></div></blockquote><div><br></div><div>I haven't repeated any name. Notice that '.callback' is different from 'callback1' or 'callback2'. That's exactly the idea—I can attach *arbitrary* callbacks later on to the '.callback' attribute.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div dir="auto">I think</div></div></blockquote><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div dir="auto"> you answered yourself here, this would not remove the existing flexible way. Just like @decorator syntax didn't remove the more flexible way. Honestly this is in my opinion almost one-to-one comparable with decorator syntax, and I don't think anyone here dares to claim decorators aren't awesome.</div></div></blockquote><div><br></div><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><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>