Movatterモバイル変換


[0]ホーム

URL:


[Python-ideas] Define a method or function attribute outside of a class with the dot operator

David Mertzmertz at gnosis.cx
Mon Feb 13 21:14:34 EST 2017


On Mon, Feb 13, 2017 at 5:48 PM, Steven D'Aprano <steve at pearwood.info>wrote:> # the clean proposed way:> def instance.method(self):  # or MyClass.method>     ...>> But I don't think that we can reasonably argue that the suggested syntax> isn't a clear, non-trivial win over the status quo, not unless we're> also going to argue that introducing decorator syntax was a waste of> time.>I argue it's not a win specifically because we HAVE decorators already.  Myoff-the-cuff `@attach_to(thing)` decorator factory missed some details onewould want, I now recognize.  We might want to attach a `.__class__`attribute, futz with `.__qualname__` and a few other things Nick pointsto.  You've been spelling this as `@inject_method(Class)`.Apart from the name of the decorator, the idea of having a *uniform* syntaxfor "put the decoration" at the top because the function might be long" isan advantage.  I agree that decorators are just syntax sugar, of course.But the new syntax sugar is just *too duplicative* of that we already have.Moreover, I think your spelling of what it is sugar for is slightly off.The `del method` at the end feels wrong to me.  Specifically, in theexample I repeated of attaching callbacks, the reason I'd want a functiondefined outside any particular class (or instance) scope is because I mightwant to use the same function as a method of various classes.  An`attach_to()` decorator can leave the defined function untouched, and onlymuck with the version that gets attached.  Hence you can even chaindecorators:@attach_to(this_menu)@attach_to(that_menu)def callback(self, ...):    ...Then later on:yet_another_menu.callback = callback-- Keeping medicines from the bloodstreams of the sick; foodfrom the bellies of the hungry; books from the hands of theuneducated; technology from the underdeveloped; and puttingadvocates of freedom in prisons.  Intellectual property isto the 21st century what the slave trade was to the 16th.-------------- next part --------------An HTML attachment was scrubbed...URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170213/0d13d930/attachment-0001.html>


More information about the Python-ideasmailing list

[8]ページ先頭

©2009-2026 Movatter.jp