Base class and Derived class question

C

cyberirakli

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)[/QUOTE]

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
 
H

Hans Mulder

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):[/QUOTE]

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):
[QUOTE]
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)[/QUOTE]

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
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top