Wouldn't it be nicer to prototype customize1()
and customize2() in the base class, as a way to
document how they should be over-ridden by their
subclasses. Something like:
class ApplicationFrameWork:
...
def customize1(): raise NotImplementedError
def customize2(): raise NotImplementedError
This seems to be the Python idiom for a "virtual function."
Expanding on jmd's entry number 1 comment: I don't believe the intent of the Template Method pattern is to always *require* execution -- so only methods in the framework that you wish to require would have the NotImplementedError exception. The others would probably have pass ...(?)
On a different matter: Why do you have
self.__templateMethod()
def __templateMethod(self):
Is it for clearer exposition or code maintenance?
Won't the following be the same behavior?
class ApplicationFramework:
def __init__(self):
for i in range(5):
self.customize1()
self.customize2()
# Create a "application":
class MyApp(ApplicationFramework):
def customize1(self):
print "Nudge, nudge, wink, wink! ",
def customize2(self):
print "Say no more, Say no more!"
MyApp()
Why are the customize functions being called five times each? I suspect you wanted to harvest numbered customize functions and call them once each:
i = 0
while 1:
i += 1
customizingFunction = 'customize%d' % (i)
if hasattr(self, customizingFunction):
apply(getattr(self, customizingFunction))
else:
break
Better yet, call all bound methods with names beginning with 'customize'...