
Hello, On Thu, 14 Jan 2021 22:19:06 +1100 Steven D'Aprano <steve@pearwood.info> wrote:
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.
Thanks for the info! But I'd say that I like syntax suggested by Chris Angelico (which parallels Ruby's syntax) better. It reminds more of the original class definition, and it's normal to add more than one var/method via mixin/interface, so grouping them together using "class ...:" makes sense. But "scoping" problems pops up nonetheless, e.g. one would like, but really can't, do following: import mod @mixin class mod.Cls: ... So, here's the syntax I came to: @mixin class Cls(mod.Cls): ... It literally reads like "let me add a mixin to class Cls (specifically, mod.Cls)". IMHO, it's pretty neat. What do you think?
Even more so, the generalisation:
def obj.method(self, args): ...
to add a method to any instance, not just to a class.
Nnnnooooo ;-). Instances can't have methods, only classes can. Instances can only have instance variable storing a reference to a method, which would need to be called (obj.method)(args), remember? ;-). [] -- Best regards, Paul mailto:pmiscml@gmail.com