[Tutor] Inheritance vs Assignment

khalil zakaria Zemmoura zemmoura.khalil at gmail.com
Thu Sep 1 10:07:53 EDT 2016


Let me clarify some ideas that I spoke about in my last email.

I talked about extending exception and linearization from the perspective
of inheriting from a class without adding behavior or some extra
attributes, because the code you provided was doing that.

Reading the Alan response made me realize that my last email wasn't very
clear and can lead to validating bad implementation.

So to clarify.

Extending classes without adding behavior can be useful in custom exception
or resolving problems with super() by linearization of the parents classes.

The implementation is a different thing.

We do that by defining an abstract class that inherit from its parent.

We do that that way
class B(A):
  pass

And no need to a second init method and calling super on it because
instantiating the B class will call implicitly the __init__ of it's parent

But as Alan mentioned before, maybe your purpose was to give just an
example to explain your question.

Regards.

Le 1 sept. 2016 11:28, "Alan Gauld via Tutor" <tutor at python.org> a écrit :

> 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
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list