Problem with method overriding from base class

G

Guest

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'
def clearScreen(self, *p):
raise 'EditInterface.clearScreen(): Interface must not be called directly'
def deleteEntry(self, *p):
raise 'EditInterface.deleteEntry(): Interface must not be called directly'
def editEntry(self, *p):
raise 'EditInterface.editEntry(): Interface must not be called directly'
def processUserInput(self, *p):
raise 'EditInterface.processUserInput(): Interface must not be called directly'
def show(self, entry, statustext):
raise 'EditInterface.show(): Interface must not be called directly'
***************************************************************************************

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);
***************************************************************************************

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' !!
Does anyone have an idea why the method of the base class is called instead of the method of the derived class and how can I do it, so that the show() of Class1 is called instead?

Greetings
Dominique

(did some data hiding there, but shouldn't really matter for the problem ;-))
 
D

Duncan Booth

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);
**********************************************************************
*
Have you considered using a dictionary mapping name to class, or even
just storing the classes directly in common.databaseEntryTypes. That way
your code would get a lot smaller and less messy: e.g. in common.py:

databaseEntryTypes = [
module1.Class1, module2.Class2, module3.Class3, module4.Class4
]

and then factory becomes:

def factory(type, *p):
return type(*p)

after which you can remove factory altogether.
Or if you keep the name -> type mapping then at least factory becomes
maintainable.
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' !! Does anyone have an idea
why the method of the base class is called instead of the method of
the derived class and how can I do it, so that the show() of Class1 is
called instead?

In the code you posted Class1 doesn't have a show() method, it has a
showEntry() method so calling show() will call the base class as the
only implementation it has.
 
B

bruno.desthuilliers

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 !-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top