Here is an example of how it could be done.
https://gist.github.com/stephanh42/97b47506e5e416f97f5790c070be7878
Stephan
Op di 31 jul. 2018 01:29 schreef Steven D'Aprano
On Tue, Jul 31, 2018 at 10:10:32AM +1200, Greg Ewing wrote:
Jamesie Pic wrote:
def o.bar(self): ...
You could get almost the same effect with
from functools import partial
def bar(self, other_args): ...
o.bar = partial(bar, o)
Why are you using functools.partial instead of types.MethodType? I'm wondering if there is some advantage to partial that I don't recognise.
I'm not sure if there's a functional difference between the two approaches, but it makes o.bar a different kind of callable and that will probably make a difference to somebody.
But IMO this is nowhere near being a common enough thing to do to justify having special syntax for it.
This sort of thing isn't common because there's no neat, easy, obvious, built-in way to do it. If we allowed people to extend classes using the syntax
def classobj.methodname(...): ...
def instance.methodname(...): ...
people would use the technique more. For good or ill.
I don't question the utility of this technique, but I suspect we prefer to *slightly* discourage it by *not* providing a Batteries Included solution for this. If you want to do this, we won't stop you, but neither will we encourage it by supporting it in syntax or providing a standard decorator for it.
-- Steve _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/