Re: [Cheetahtemplate-discuss] Overriding awake() not working
Brought to you by:
rtyler,
tavis_rudd
From: Edmund L. <el...@in...> - 2002-04-07 20:26:25
|
I wrote: >Are you sure this is right? AFAIK, there are no implied semantics >in Python, in the order of base class definitions when multiple >inheritance is concerned. The only reference I've ever seen to >this is that there is a depth-first, left to right search through >the base class list when something needs to be resolved. E.g.: > http://www.python.org/doc/current/tut/node11.html#SECTION0011510000000000000000 > >The problem here seems to be that my SiteTemplate (and everyone's >SiteTemplates for that matter) necessarily define an awake() method, >so that any base class that we tack on after it cannot override >awake() or any other method already defined. This is a nasty one >since programmers now need to be aware of all the methods, instance >and class variables being defined in SiteTemplate and its bases(*). > >We can change the declaration order for the base classes so that a >mix-in is the first base class and SiteTemplate follows later. This >should probably fix the override issue, but it causes a different >problem--Cheetah calls the __init__ of only the first class it sees, >so that the servlet is not inited properly. > >Looks like this multiple inheritance thing is a real problem. We can't >do form processing, etc. by overriding the awake() and sleep() methods >as documented in the UG. I've been educating myself over the weekend about this issue. It's not a new one by any means. What we're really noticing here are two separate problems that are discussed at length in the literature. The first problem is one of inheritance versus composition (delegation). Inheritance results in rigid interfaces and weak encapsulation of attributes, methods, etc. of the superclasses, hence the need to be aware of all attributes and methods in superclasses when writing a Cheetah template or mix-in for use with the template. There are other pros and cons of the approaches, revolving around fragility of superclass interfaces, etc. This is a good, short summary of some of these issues: http://www.javaworld.com/javaworld/jw-11-1998/jw-11-techniques.html The second problem is a generic multiple inheritance problem that is recognized and addressed in Python 2.2. Kuchling discusses this in http://www.amk.ca/python/2.2/index.html#SECTION000330000000000000000" but there are lots of references to this in the literature going back to the early days of OO. Here's a good reference: http://www.elj.com/eiffel/feature/inheritance/mi/review/ It seems that the best way out of this is either to use the Python 2.2 resolution mechanism (copied from CLOS), or perhaps just composition. It could be that just switching to Python 2.2 would solve the problem I had in overriding awake(), without requiring any changes to the Cheetah compiler. OTOH, I'll try composition in a page template or two to see how it feels since solving the multiple inheritance problem still doesn't give the strong encapsulation that would certainly simplify life. ...Edmund. |