[Tutor] Inheritance vs Assignment

Alan Gauld alan.gauld at yahoo.co.uk
Thu Sep 1 06:24:57 EDT 2016


On 01/09/16 04:18, kay Cee wrote:

> Class a():
>      def__init__(self, var):
>             pass
> 
> Class b(a):
>      def__init__(self):
>      super().__init__(self, var)
>             pass

> Is it better to do
> 
> b = a()
> 
> Instead of making b its own class?
> Also, what would be the benefit of making a separate class for b if any at all?

I'm afraid your question is too abstract to answer.
The use of classes depends on the context and in this
case we have no context on which to base a judgement.

Looking strictly at your code it makes no sense to
have either a or b as classes since there is no
state (variables) involved and no operations(methods)
are provided. Both are zero impact classes. But I'm
guessing you intended that to be indicative of a
more complex scenario.

In general, inheritance is used to specialise a class.
It should be true to say that b *is an* a.
So if b were to add some specialized content
to 'a' then inheritance would make sense.

But b = a() makes b an instance of 'a' not a class.
So the two things are entirely different, it's like
asking which is better: a mountain or Mount Everest?


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list