[Python-ideas] Composition over Inheritance

Soni L. fakedme+py at gmail.com
Sat Oct 28 07:09:30 EDT 2017

As recent threads indicate, composition may sometimes be better than 
inheritance. And so I'd like to propose composition as a built-in feature.

My idea is syntax of the form o.[c].m(), where o is an object, c is a 
component, m is a method.

I am not sure how you'd set components, or test for components, but I 
don't think it makes sense to be able to do o.[c][x] or x=o.[c], because 
those break the idea of automatically passing the object as an argument 
(unless we create whole wrapper objects every time the syntax is used, 
and that's a bit ew. also how would you handle o.[c1].[c2] ?).

Thus I think o.[c].m() should be syntax sugar for o[c].m(o), with o 
being evaluated only once, as that solves a lot of current issues 
relating to inheritance while introducing very few issues relating to 
python's "everything is separate" (e.g. __get__ vs __getattr__) 
policy.This also makes setting components and testing for components 
fairly trivial, and completely avoids the issues mentioned above by 
making their syntax illegal.

(Disclaimer: This was inspired by my own programming language, 
Cratera[1], so I'm a bit biased here. Cratera was, in turn, inspired by 
Rust[2] traits. Note however that the original plans for Cratera were 
far more flexible, including allowing the "problematic" o.[c1].[c2].m() 
and o.[c][x].m(). I can go into more detail on how those should work, if 
wanted, but implementation-wise it's not looking good.)

[1] https://bitbucket.org/TeamSoni/cratera
[2] https://www.rust-lang.org/

More information about the Python-ideas mailing list