[Python-ideas] Composition over Inheritance

Brendan Barnwell brenbarn at brenbarn.net
Sun Oct 29 12:57:53 EDT 2017


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?

-- 
Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is no 
path, and leave a trail."
    --author unknown


More information about the Python-ideas mailing list