Problem with method overriding from base class

Discussion in 'Python' started by Guest, Mar 31, 2008.

  1. Guest

    Guest 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 ;-))
     
    Guest, Mar 31, 2008
    #1
    1. Advertising

  2. Guest

    Duncan Booth Guest

    <> 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.
     
    Duncan Booth, Mar 31, 2008
    #2
    1. Advertising

  3. Guest

    Guest

    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 !-)
     
    , Mar 31, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stuart McGraw
    Replies:
    10
    Views:
    477
    Scott David Daniels
    Feb 17, 2004
  2. alf

    overriding base class

    alf, Jun 30, 2007, in forum: Python
    Replies:
    2
    Views:
    297
    Michele Simionato
    Jun 30, 2007
  3. James Emil Avery

    Overriding base class

    James Emil Avery, Jul 25, 2007, in forum: C++
    Replies:
    4
    Views:
    384
  4. Karan Rajput
    Replies:
    2
    Views:
    150
    Abinoam Jr.
    Dec 22, 2010
  5. Koszalek Opalek

    Overriding a class method with an object method

    Koszalek Opalek, Jul 2, 2007, in forum: Perl Misc
    Replies:
    8
    Views:
    156
    -berlin.de
    Jul 6, 2007
Loading...

Share This Page