Starting up with Python – Part14

Welcome to another Python article on Subclasses / Superclasses. I will also get into Ovewrite Variable on Sub as well as Multiple Parent Classes. Here is a mind map of all articles related to Python.



This is interesting when you can inherit a single parent class from a class.

>> class parentClass: ... var1="country" ... var2="region" ... >>> class childClass(parentClass): ... pass ... >>> parentObject=parentClass() >>> parentObject.var1 'country' >>> childObject=childClass() >>> childObject.var1 'country' >>> childObject.var2 'region'

33. Overwrite Variable on Sub

We will now try to overwrite variable on subclass. I will create a parent class called hackers and the child class called project.

>> class hackers:... var1="country"... var2="age"... >>> class project(hackers):... var2="skills"

Let's access var2 from the parent and the child.

>> parentobject=hackers()>>> childobject=project()>>> parentobject.var1'country'>>> childobject.var2'skills

As we can see now if we try to overwrite its just by calling the variable in the class

>> childobject.var1'country'

This is cool if you have one huge class and if you want to change some few things in it.

34. Multiple Parent Classes

This is straight forward to use different items just by adding the name of your classes in your parameter.

>> class hackers:... var1="Mauritius"... >>> class skills:... var2="devops"... >>> class best(hackers,skills):... var3="we are hackers"... >>> childObject=best()>>> childObject.var1'Mauritius'>>> childObject.var2'devops'>>> childObject.var3'we are hackers'