another question about classes and subclassing

Alex Hall mehgcap at gmail.com
Tue May 18 22:12:37 EDT 2010


On 5/18/10, Dave Angel <davea at ieee.org> wrote:
> 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.
Sorry, top-posting is a habit on some other lists I am on, and
sometimes it follows me to lists where in-line posting is the way to
do it.
>
> 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.
So that is the only way of making submarine inherit craft, by calling
craft's __init__ inside submarine's __init__. Okay, I think I get it.
>
> 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.
Very true.
> 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.
Right, I think I have it now... At least I hope I do!
>
> Incidentally, convention is to capitalize class names.
Yes, I had planned on doing that in my project, but I got caught up in
just making it work and forgot to do so. Thanks for the reminder.
>
> DaveA
>
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap



More information about the Python-list mailing list