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
>>> childObject=childClass()
>>> childObject.var1
>>> childObject.var2

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
>>> childobject.var2

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

>> childobject.var1

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
>>> childObject.var2
>>> childObject.var3
‘we are hackers’

Nitin J Mutkawoa

Blogger at | Founding member of | An Aficionado Journey in Opensource & Linux – And now It's a NASDAQ touch!

You May Also Like

More From Author