Re: Problem with method overriding from base class
- From: "bruno.desthuilliers@xxxxxxxxx" <bruno.desthuilliers@xxxxxxxxx>
- Date: Mon, 31 Mar 2008 13:37:33 -0700 (PDT)
On 31 mar, 11:05, <Dominique.Holzwa...@xxxxxxxxxxxxxx> wrote:
Hello everyone
I have defined some sort of 'interface class' and a factory function that creates instance objects of specific classes, which implement that interface:
Interface definition:
***************************************************************************************
import GUI.webGUI as webGUI
class EditInterface(webGUI.WebGUI):
def addEntry(self, *p):
raise 'EditInterface.addEntry(): Interface must not be called directly'
You want:
raise NotImplementedError('EditInterface.addEntry():
Interface must not be called directly')
And unless you have some pretty good reason to do so (ie: template
methods in EditInterface depending on these methods), you don't even
want to bother with all this - just document which methods must be
implemented, and let Python raise an AttributeError if they are not.
(snip)
Factory:
***************************************************************************************
def factory(type, *p):
if type == common.databaseEntryTypes[0]:
return module1.Class1(*p);
elif type == common.databaseEntryTypes[1]:
return module2.Class2(*p);
elif type == common.databaseEntryTypes[2]:
return module3.Class3(*p);
elif type == common.databaseEntryTypes[3]:
return module4.Class4(*p);
The whole point of polymorphic dispatch in OO is to avoid this kind of
mess. What's wrong with instanciating classes directly ? NB : in
Python, classes are objects too, so you can pass them around as
needed. Also, instanciation is done thu a call to the class object -
which makes it just the same as a function call. IOW, you just don't
need a clumsy dedicated factory function just to make sure the client
code is not too tightly coupled to the exact implementation.
Implementing Class1:
***************************************************************************************
import editInterface
class Class1(editInterface.EditInterface):
def __init__(self, product, database):
# do something here ...
def showEntry(self, entry, statustext):
# do something here as well, return some string...
***************************************************************************************
Now, when I want to create an Instance of Class1 I do:
myClass1Instance = factory.factory(common.databaseEntryTypes[1], 'Name', databaseObj )
Which seems to work fine according to the debugger. But when I do next:
msg = myClass1Instance.show(firstEntry, '')
Then the show() method of the class 'EditInterface' is called instead of the show() method of the class 'Class1' !!
Reread your code : class Class1 have no 'show' method !-)
.
- References:
- Problem with method overriding from base class
- From: Dominique.Holzwarth
- Problem with method overriding from base class
- Prev by Date: Re: python scripts to standalone executable
- Next by Date: Re: Newbie Question - Overloading ==
- Previous by thread: Re: Problem with method overriding from base class
- Next by thread: wxPython; adding a grid to a panel
- Index(es):
Relevant Pages
|