On Thu, Jan 14, 2021 at 02:05:50PM +0300, Paul Sokolovsky wrote:

[...]
> Semantically, Python can achieve the same with "imperative" syntax like:
> 
> def mixin_method(self, args):
>     ...
> Cls.mixin_method = mixin_method
> 
> 
> The question then: what are the best practices in *declarative* syntax
> to achieve the same effect in Python? (but of course, unlike Ruby,
> there should be explicit syntactic marker that we augment existing
> class, not redefine it).

    def Cls.mixin_method(self, args):
        ...

has been suggested as syntax for adding new methods to an existing 
class. I would use that occasionally.

Even more so, the generalisation:

    def obj.method(self, args):
        ...

to add a method to any instance, not just to a class. (Assuming that the 
instance has a writable `__dict__` of course -- you can't add attributes 
to a float or tuple, for example.)

The advantage is that there is no new keyword required.



-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/FOG24VZMOCBKEI5NIHWNP2J2NTVLP4CS/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to