Problem with method overriding from base class

Duncan Booth duncan.booth at invalid.invalid
Mon Mar 31 11:32:37 CEST 2008


<Dominique.Holzwarth at ch.delarue.com> wrote:

 
> 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.



More information about the Python-list mailing list