On Sat, Sep 12, 2020 at 07:25:30PM -0400, Eric V. Smith wrote:
> On 9/12/2020 7:13 PM, Random832 wrote:
> >On Fri, Sep 11, 2020, at 19:57, Cameron Simpson wrote:
> >>The default (an instance method) requires "self" to perform.
> >Of course, this is only the default if the method is a function object. If
> >it is a different callable class, the default is effectively staticmethod.
> >
> >Perhaps there should be an @instancemethod?
>
> What would that let us do that we can't currently achieve?
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:
py> class K:
... pass
...
py> obj = K()
py> obj.method = lambda self, x: (self, x)
py> obj.method('arg') # Fails
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: <lambda>() missing 1 required positional argument: 'x'
But this works:
py> obj.method = MethodType(lambda self, x: (self, x), obj)
py> obj.method('arg')
(<__main__.K object at 0x7f67a4e051d0>, 'arg')
So an instancemethod decorator would be a waste of time, but the
instancemethod type, spelled types.MethodType, is very useful.
--
Steve
_______________________________________________
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/D24PW37YSIHV2XHNHARQLM4EMHH6GQKD/
Code of Conduct: http://python.org/psf/codeofconduct/