Classes - "delegation" question.

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Dec 18 06:03:20 EST 2014


dieter wrote:

> "Ivan Evstegneev" <webmailgroups at gmail.com> writes:
>> I have a question about "delegation" coding pattern(I'm working with
>> Python 3.4).
> 
> Unlike Java, Python supports "multiple inheritance". This means
> that you need "delegation" much more rarely in Python.
> Python does not have much special support for delegation: usually,
> you must delegate explicitely - maybe using a delagating decorator.


You might *need* delegation more rarely, but some people believe that
delegation and composition is a better design pattern than inheritance:

http://learnpythonthehardway.org/book/ex44.html

http://en.wikipedia.org/wiki/Composition_over_inheritance#Benefits

http://joostdevblog.blogspot.com.au/2014/07/why-composition-is-often-better-than.html

http://stackoverflow.com/questions/2068158/why-does-cocoa-use-delegates-rather-than-inheritance?lq=1


However, be warned that there are two subtly different models for
delegation. Here's the one that people seem to forget:

http://code.activestate.com/recipes/519639-true-lieberman-style-delegation-in-python/



-- 
Steven




More information about the Python-list mailing list