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

Search Discussions

  • Duncan Booth at Mar 31, 2008 at 9:32 am

    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.
  • Bruno Desthuilliers at Mar 31, 2008 at 8:37 pm

    On 31 mar, 11:05, 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 !-)

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMar 31, '08 at 9:05a
activeMar 31, '08 at 8:37p
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase