On 6/11/12 14:47:03, cyberira...@gmail.com 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"[/CODE] > > derivedClass.py: > [CODE]import baseClass as baseClassMod > reload(baseClassMod) > > class derivedClass(baseClassMod):
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): > def dFunction(self): > print "We are in a derived Class" [/CODE] > > 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][/CODE] > > 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"[/CODE] > 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 -- http://mail.python.org/mailman/listinfo/python-list