[Tutor] Inheritance vs Assignment

khalil zakaria Zemmoura zemmoura.khalil at gmail.com
Thu Sep 1 06:15:35 EDT 2016


Assignment and inheritance are not comparable at all
In the inheritance you are extending the base class (a) and in "a=b()" you
are instantiating it - you create an object according to the blueprint that
you difining.

To understand the difference.
Inheritance:
Say you defined a costume type (a class ) and classes are really that.
After that you needed  an other kind of object that share the same code
with  "class a" with additional attribute and actions you want to apply to
them. Instead of duplicating the code in "b" just inherit from "a" and
write your extra code in "b" and you are done. You still need to
instantiate "b" to get an object and work with.

Assignment is a different thing that doesn't have any thing to do with
inheritance, since what you are doing here is creating an object in the
memory and binding it to a name to access it later.

Is that make sense?

Now I can see some interest in using the  "b inherit from a" that way.

It's a common technique to just inherit from an exception class to just
catch an exception with a meaningful name.

The second case that I see is that when using multiple inheritance that's
allow you to linearize the parents classes to get the code works.

Since I am responding from a smartphone, It's tedious to expand the
explanation, I wanted to give you an overview of some concepts and ideas on
how they work.

Google working with exceptions and you'll get a tons of tutorials that
shows you how it works.

And search a talk about "super" named "super consider super". It'll give
you more information about the inheritance model of Python, especially the
multiple inheritance model and the "method resolution order called 'mro' ".

I hope that helps you. If it doesn't, just ask again.

Regards.

Le 1 sept. 2016 09:51, "kay Cee" <unee0x at gmail.com> a écrit :

>
> Suppose there is ----->
>
> Class a():
>      def__init__(self, var):
>             pass
>
> Class b(a):
>      def__init__(self):
>      super().__init__(self, var)
>             pass
>
> Note: syntax may be incorrect ...
>
> 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?
>
> Thanks in advance ....
>
> Unee0x
> _______________________________________________
> 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