[Python-ideas] Composition over Inheritance

Soni L. fakedme+py at gmail.com
Sun Oct 29 14:25:12 EDT 2017



On 2017-10-29 02:57 PM, Brendan Barnwell wrote:
> On 2017-10-29 04:44, Soni L. wrote:
>> And this is how you miss the whole point of being able to dynamically
>> add/remove arbitrary components on objects you didn't create, at 
>> runtime.
>>
>> Someone gave me this code and told me it explains what I'm trying to do:
>> https://repl.it/NYCF/3
>>
>> class T:
>>      pass
>>
>> class C:
>>      pass
>>
>> c = C()
>>
>> #c.[T] = 1
>> c.__dict__[T] = 1
>
>     Again, can you please explain why you want to write c.[T]? What do 
> you intend that to *do*?  Your commented line seems to indicate you 
> want it to do what `c.__dict__[T]` does, but you can already do that 
> with `setattr(c, T, 1)`.  Or you can just give c an attribute that's a 
> dict, but has an easier-to-type name than __dict__, so you can do 
> `c.mydict[T]`.  What is the specific advantage of `c.[T]` over these 
> existing solutions?
>

Hmm... Why can't we just allow empty identifiers, and set a default 
handler for empty identifiers that implements the proposed ECS?

But the basic idea is to indicate something at the call site, namely 
that T is a contract and the object returned should respect that 
contract and any function calls should pass the original object as an 
argument. (I personally don't like how Python treats o.m() (has self) 
the same as o.f() (no self) syntax-wise, either.)


More information about the Python-ideas mailing list