Base class and Derived class question

Discussion in 'Python' started by cyberirakli@gmail.com, Nov 6, 2012.

  1. Guest

    Hey guys,
    I'm trying to understand how is working base class and derived class.
    So, I have to files baseClass.py and derivedClass.py.
    baseClass.py :
    Code:
    class baseClass():
    def bFunction(self):
    print "We are in a base class"
    derivedClass.py:
    Code:
    import baseClass as baseClassMod
    reload(baseClassMod)
    
    class derivedClass(baseClassMod):
    def dFunction(self):
    print "We are in a derived Class" 
    buwhen I'm trying to run derivedClass.py I get this error :
    Code:
    [COLOR=Red]TypeError: Error when calling the metaclass bases
    module.__init__() takes at most 2 arguments (3 given)[/COLOR]
    Interesting thing is that if I run baseClass.py and then run :
    Code:
    class derivedClass(baseClass):
    def dFunction(self):
    print "We are in a derived Class"
    It works fine
    , Nov 6, 2012
    #1
    1. Advertising

  2. Hans Mulder Guest

    On 6/11/12 14:47:03, wrote:
    > Hey guys,
    > I'm trying to understand how is working base class and derived class.
    > So, I have to files baseClass.py and derivedClass.py.
    > baseClass.py :
    >
    Code:
    class baseClass():
    >     def bFunction(self):
    >         print "We are in a base class"
    >
    > derivedClass.py:
    >
    Code:
    import baseClass as baseClassMod
    > reload(baseClassMod)
    >
    > class derivedClass(baseClassMod):[/color]
    
    This line is wrong: baseClassMod is a module, not a class.
    You cannot derive a class from a module, only from another class.
    
    If you import baseClass as baseClassMod, then the name of the class
    defined inside the module is baseClassMod.baseClass, so this line
    should be:
    
    class derivedClass(baseClassMod.baseClass):
    [color=blue]
    >     def dFunction(self):
    >         print "We are in a derived Class" 
    >
    > buwhen I'm trying to run derivedClass.py I get this error :
    >
    Code:
    [COLOR=Red]TypeError: Error when calling the metaclass bases
    >     module.__init__() takes at most 2 arguments (3 given)[/COLOR]
    >
    > Interesting thing is that if I run baseClass.py and then run :
    >
    Code:
    class derivedClass(baseClass):
    >     def dFunction(self):
    >         print "We are in a derived Class"
    > It works fine


    Alternative solutions that also work:

    import baseClass

    class derivedClass(baseClass.baseClass):
    def dFunction(self):
    print "We are in a derived Class"

    Or you can import the class rather than the module:

    from baseClass import baseClass

    class derivedClass(baseClass):
    def dFunction(self):
    print "We are in a derived Class"


    If you're trying to learn about derived classes, then it might
    be a good idea to avoid learning about the pitfalls of importing
    at the same time. If you simply put both classes in the same
    file, the problem disappears:

    class baseClass():
    def bFunction(self):
    print "We are in a base class"

    class derivedClass(baseClass):
    def dFunction(self):
    print "We are in a derived Class"

    instance = derivedClass()
    instance.bFunction()
    instance.dFunction()

    This works as you'd expect.


    Incidentally, the recommended way to avoid confusion between modules
    and classes it to use lower case names for your modules abd names
    with an initial capital for your classes, for example:

    class BaseClass(object):
    def bMethod(self):
    print "We are in a base class"

    class DerivedClass(BaseClass):
    def dMethod(self):
    print "We are in a derived Class"

    instance = DerivedClass()
    instance.bMethod()
    instance.dMethod()


    Hope this helps,

    -- HansM
    Hans Mulder, Nov 6, 2012
    #2
    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. Replies:
    4
    Views:
    387
    Alf P. Steinbach
    May 23, 2007
  2. Replies:
    1
    Views:
    378
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    369
    Victor Bazarov
    May 23, 2007
  4. Replies:
    2
    Views:
    685
  5. Replies:
    7
    Views:
    207
    alex23
    Nov 7, 2012
Loading...

Share This Page