Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32818 > unrolled thread
| Started by | cyberirakli@gmail.com |
|---|---|
| First post | 2012-11-06 05:47 -0800 |
| Last post | 2012-11-06 17:33 +0100 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
Base class and Derived class question cyberirakli@gmail.com - 2012-11-06 05:47 -0800
Re: Base class and Derived class question Hans Mulder <hansmu@xs4all.nl> - 2012-11-06 17:33 +0100
| From | cyberirakli@gmail.com |
|---|---|
| Date | 2012-11-06 05:47 -0800 |
| Subject | Base class and Derived class question |
| Message-ID | <31d86df5-8cfe-46b8-ae54-84fbfd82e98e@googlegroups.com> |
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):
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
[toc] | [next] | [standalone]
| From | Hans Mulder <hansmu@xs4all.nl> |
|---|---|
| Date | 2012-11-06 17:33 +0100 |
| Message-ID | <50993bc7$0$6967$e4fe514c@news2.news.xs4all.nl> |
| In reply to | #32818 |
On 6/11/12 14:47:03, cyberirakli@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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web