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 :
    >>> class baseClass():

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

    derivedClass.py:
    >>>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 :
    TypeError: Error when calling the metaclass bases
    module.__init__() takes at most 2 arguments (3 given)

    Interesting thing is that if I run baseClass.py and then run :
    >>>class derivedClass(baseClass):

    def dFunction(self):
    print "We are in a derived Class"
    It works fin
    , Nov 6, 2012
    #1
    1. Advertising

  2. Guest

    > in what Python version ?

    Python 2.7.3


    > How did all those angle brackets get into the file? Are you confusing
    >
    > an interactive interpreter session with running source files?


    I've used angle brackets just for posting here,becauze this forum doesn't support
    Code:
    
    
    I have a file called baseClass.py with code abouve and indentation is correct.
    Then I open python IDLE and type :
    import baseClass as baseClassMod
    reload(baseClassMod

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

    After that i get the error above. But if I paste in Python IDLE a code from baseClass.py and just run:
    class derivedClass(baseClass):
    def dFunction(self):
    print "We are in a derived Class"

    it works perfectly.
    Why happen this?
    , Nov 6, 2012
    #2
    1. Advertising

  3. Guest

    > in what Python version ?

    Python 2.7.3


    > How did all those angle brackets get into the file? Are you confusing
    >
    > an interactive interpreter session with running source files?


    I've used angle brackets just for posting here,becauze this forum doesn't support
    Code:
    
    
    I have a file called baseClass.py with code abouve and indentation is correct.
    Then I open python IDLE and type :
    import baseClass as baseClassMod
    reload(baseClassMod

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

    After that i get the error above. But if I paste in Python IDLE a code from baseClass.py and just run:
    class derivedClass(baseClass):
    def dFunction(self):
    print "We are in a derived Class"

    it works perfectly.
    Why happen this?
    , Nov 6, 2012
    #3
  4. Guest

    Just got answer, I didn't call a class it's self. Correct code is:
    class derivedClass(baseClassMod.baseClass):
    def ......
    , Nov 6, 2012
    #4
  5. Ian Kelly Guest

    On Tue, Nov 6, 2012 at 8:03 AM, <> wrote:
    > I've used angle brackets just for posting here,becauze this forum doesn't support
    Code:
    
    


    This is a Usenet group, not a web forum.

    > Just got answer, I didn't call a class it's self. Correct code is:
    > class derivedClass(baseClassMod.baseClass):
    > def ......


    Better style would be to import the class from the module in the first place:

    from baseClass import baseClass

    # ...

    class derivedClass(baseClass):
    # ...

    Better yet would be to put both classes in the same file in the first
    place. Python isn't Java, where each class is an independent
    compilation unit. There is no reason to put each class in its own
    separate module, and it tends to cause namespace confusion as you have
    discovered.
    Ian Kelly, Nov 6, 2012
    #5
  6. Guest

    On Tuesday, November 6, 2012 4:35:47 PM UTC+1, Ian wrote:
    > On Tue, Nov 6, 2012 at 8:03 AM,
    >
    > > I've used angle brackets just for posting here,becauze this forum doesn't support
    Code:
    
    

    >
    >
    >
    > This is a Usenet group, not a web forum.
    >
    >
    >
    > > Just got answer, I didn't call a class it's self. Correct code is:

    >
    > > class derivedClass(baseClassMod.baseClass):

    >
    > > def ......

    >
    >
    >
    > Better style would be to import the class from the module in the first place:
    >
    >
    >
    > from baseClass import baseClass
    >
    >
    >
    > # ...
    >
    >
    >
    > class derivedClass(baseClass):
    >
    > # ...
    >
    >
    >
    > Better yet would be to put both classes in the same file in the first
    >
    > place. Python isn't Java, where each class is an independent
    >
    > compilation unit. There is no reason to put each class in its own
    >
    > separate module, and it tends to cause namespace confusion as you have
    >
    > discovered.




    On Tuesday, November 6, 2012 4:35:47 PM UTC+1, Ian wrote:
    > On Tue, Nov 6, 2012 at 8:03 AM,
    >
    > > I've used angle brackets just for posting here,becauze this forum doesn't support
    Code:
    
    

    >
    >
    >
    > This is a Usenet group, not a web forum.
    >
    >
    >
    > > Just got answer, I didn't call a class it's self. Correct code is:

    >
    > > class derivedClass(baseClassMod.baseClass):

    >
    > > def ......

    >
    >
    >
    > Better style would be to import the class from the module in the first place:
    >
    >
    >
    > from baseClass import baseClass
    >
    >
    >
    > # ...
    >
    >
    >
    > class derivedClass(baseClass):
    >
    > # ...
    >
    >
    >
    > Better yet would be to put both classes in the same file in the first
    >
    > place. Python isn't Java, where each class is an independent
    >
    > compilation unit. There is no reason to put each class in its own
    >
    > separate module, and it tends to cause namespace confusion as you have
    >
    > discovered.




    On Tuesday, November 6, 2012 4:35:47 PM UTC+1, Ian wrote:

    >
    > > I've used angle brackets just for posting here,becauze this forum doesn't support
    Code:
    
    

    >
    >
    >
    > This is a Usenet group, not a web forum.
    >
    >
    >
    > > Just got answer, I didn't call a class it's self. Correct code is:

    >
    > > class derivedClass(baseClassMod.baseClass):

    >
    > > def ......

    >
    >
    >
    > Better style would be to import the class from the module in the first place:
    >
    >
    >
    > from baseClass import baseClass
    >
    >
    >
    > # ...
    >
    >
    >
    > class derivedClass(baseClass):
    >
    > # ...
    >
    >
    >
    > Better yet would be to put both classes in the same file in the first
    >
    > place. Python isn't Java, where each class is an independent
    >
    > compilation unit. There is no reason to put each class in its own
    >
    > separate module, and it tends to cause namespace confusion as you have
    >
    > discovered.




    Thank you for reply. Of course, import just a class from the module. The reason of have each class in separate file is that I have a base class with basic functionality and a lot of derived classes from it with custom functionality for each class. Also, the program is modular and periodically willneed adding some new modules. So, for better organisation of all this stuff I have put them in separate files.
    , Nov 6, 2012
    #6
  7. Guest

    On Tuesday, November 6, 2012 4:35:47 PM UTC+1, Ian wrote:
    > On Tue, Nov 6, 2012 at 8:03 AM,
    >
    > > I've used angle brackets just for posting here,becauze this forum doesn't support
    Code:
    
    

    >
    >
    >
    > This is a Usenet group, not a web forum.
    >
    >
    >
    > > Just got answer, I didn't call a class it's self. Correct code is:

    >
    > > class derivedClass(baseClassMod.baseClass):

    >
    > > def ......

    >
    >
    >
    > Better style would be to import the class from the module in the first place:
    >
    >
    >
    > from baseClass import baseClass
    >
    >
    >
    > # ...
    >
    >
    >
    > class derivedClass(baseClass):
    >
    > # ...
    >
    >
    >
    > Better yet would be to put both classes in the same file in the first
    >
    > place. Python isn't Java, where each class is an independent
    >
    > compilation unit. There is no reason to put each class in its own
    >
    > separate module, and it tends to cause namespace confusion as you have
    >
    > discovered.




    On Tuesday, November 6, 2012 4:35:47 PM UTC+1, Ian wrote:
    > On Tue, Nov 6, 2012 at 8:03 AM,
    >
    > > I've used angle brackets just for posting here,becauze this forum doesn't support
    Code:
    
    

    >
    >
    >
    > This is a Usenet group, not a web forum.
    >
    >
    >
    > > Just got answer, I didn't call a class it's self. Correct code is:

    >
    > > class derivedClass(baseClassMod.baseClass):

    >
    > > def ......

    >
    >
    >
    > Better style would be to import the class from the module in the first place:
    >
    >
    >
    > from baseClass import baseClass
    >
    >
    >
    > # ...
    >
    >
    >
    > class derivedClass(baseClass):
    >
    > # ...
    >
    >
    >
    > Better yet would be to put both classes in the same file in the first
    >
    > place. Python isn't Java, where each class is an independent
    >
    > compilation unit. There is no reason to put each class in its own
    >
    > separate module, and it tends to cause namespace confusion as you have
    >
    > discovered.




    On Tuesday, November 6, 2012 4:35:47 PM UTC+1, Ian wrote:

    >
    > > I've used angle brackets just for posting here,becauze this forum doesn't support
    Code:
    
    

    >
    >
    >
    > This is a Usenet group, not a web forum.
    >
    >
    >
    > > Just got answer, I didn't call a class it's self. Correct code is:

    >
    > > class derivedClass(baseClassMod.baseClass):

    >
    > > def ......

    >
    >
    >
    > Better style would be to import the class from the module in the first place:
    >
    >
    >
    > from baseClass import baseClass
    >
    >
    >
    > # ...
    >
    >
    >
    > class derivedClass(baseClass):
    >
    > # ...
    >
    >
    >
    > Better yet would be to put both classes in the same file in the first
    >
    > place. Python isn't Java, where each class is an independent
    >
    > compilation unit. There is no reason to put each class in its own
    >
    > separate module, and it tends to cause namespace confusion as you have
    >
    > discovered.




    Thank you for reply. Of course, import just a class from the module. The reason of have each class in separate file is that I have a base class with basic functionality and a lot of derived classes from it with custom functionality for each class. Also, the program is modular and periodically willneed adding some new modules. So, for better organisation of all this stuff I have put them in separate files.
    , Nov 6, 2012
    #7
  8. alex23 Guest

    On Nov 7, 1:08 am, wrote:
    > Just got answer, I didn't call a class it's self.  Correct code is:
    > class derivedClass(baseClassMod.baseClass):
    >     def ......


    Incidentally, this is why it's recommended to give modules lowercase
    names - baseclass - and classes camelcased ones - BaseClass. It makes
    it more obvious to what you're holding a reference.
    alex23, Nov 7, 2012
    #8
    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:
    1
    Views:
    207
    Hans Mulder
    Nov 6, 2012
Loading...

Share This Page