another question about classes and subclassing

Dave Angel davea at ieee.org
Tue May 18 22:01:07 EDT 2010


Alex Hall wrote:
> Okay, that makes sense. So by calling submarine(craft) I am bringing
> in all of craft's attribs (subclassing)? Or does calling craft's
> __init__ method do that instead? Is there an advantage to doing it
> this way, rather than just making separate classes for everything,
> except for my own sense of organization; speed, memory usage, less
> coding? Thanks.
>
> <snip>
>>     
You put your response at the wrong end of the message.  It should 
*follow* whatever you're quoting.  So in your case, I'm forced to delete 
everything just to make the order correct.

You don't call Submarine(Craft), as it's not a function.  That's a class 
definition.  You instantiate a Submarine by something like:

sub = Submarine(arg1, arg2, arg3)

And those three arguments are then passed to the __init__() method of 
Submarine.  It may choose to call the __init__() method of Craft, in the 
method.

There are lots of advantages of using inheritance.  First, any common 
code only has to be entered once, so you'll only have to fix it once 
when you find mistakes, or decide to redesign.  Second, it's easier to 
read - very important.  It may take less memory, but probably not.  And 
it's not likely to have any speed impact.

Craft instances don't have any instance data attributes if nobody 
assigns them.  And that's normally the job of __init__().  So naturally, 
you need to call it from the Submarine class.

Incidentally, convention is to capitalize class names.

DaveA




More information about the Python-list mailing list