[Python-ideas] Define a method or function attribute outside of a class with the dot operator
mertz 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>
> # 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
I argue it's not a win specifically because we HAVE decorators already. My
off-the-cuff `@attach_to(thing)` decorator factory missed some details one
would want, I now recognize. We might want to attach a `.__class__`
attribute, futz with `.__qualname__` and a few other things Nick points
to. You've been spelling this as `@inject_method(Class)`.
Apart from the name of the decorator, the idea of having a *uniform* syntax
for "put the decoration" at the top because the function might be long" is
an 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 the
example I repeated of attaching callbacks, the reason I'd want a function
defined outside any particular class (or instance) scope is because I might
want to use the same function as a method of various classes. An
`attach_to()` decorator can leave the defined function untouched, and only
muck with the version that gets attached. Hence you can even chain
def callback(self, ...):
Then later on:
yet_another_menu.callback = callback
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas