On Sat, Sep 12, 2020, at 23:14, Steven D'Aprano wrote:
> We already have an instancemethod, it's just spelled differently:
>
> py> from types import MethodType
>
>
> And while it is not useful as a decorator, it is *really* useful for
> adding methods to an individual instance rather than the class:
This isn't what I was suggesting - I meant something like this:
class instancemethod:
def __init__(self, wrapped):
self.wrapped = wrapped
def __get__(self, obj, objtype):
if obj is None: return self.wrapped
else: return MethodType(self.wrapped, obj)
this wouldn't be useful for functions, but would give other callables the same
functionality as functions, automatically creating the bound method object,
e.g.:
class D:
def __init__(self, obj, *args): ...
class C:
foo = instancemethod(D)
obj = C()
obj.foo(...) # D(obj, ...)
Same for other types of callables such as functools.partial objects etc.
> So an instancemethod decorator would be a waste of time, but the
> instancemethod type, spelled types.MethodType, is very useful.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/CYBMRSMOQOKSYELNW7AZ36WL3W23RD5Q/
Code of Conduct: http://python.org/psf/codeofconduct/