Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Base class and Derived class question
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Hans Mulder  
View profile  
 More options Nov 6 2012, 11:33 am
Newsgroups: comp.lang.python
From: Hans Mulder <han...@xs4all.nl>
Date: Tue, 06 Nov 2012 17:33:10 +0100
Local: Tues, Nov 6 2012 11:33 am
Subject: Re: Base class and Derived class question
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »