Python Inheritance

I was once told to work smarter, and not harder in order to improve my performance. This is the core concept of what “Inheritance” is within Python. Inheritance allows you to:

“Define attributes and objects within a class, and then create another class that can access those same attributes”

To paraphrase, if you have a chef class that can make_chicken as a function, you can create an Italian chef who can use that make_chicken function and more, without having to copy and paste it.

Below you will see a Chef class. The average Chef can make a chicken dish, a salad dish, and a special dish which is BBQ.

In order to use the Chef functions in the new Italian Class, two things are required. Import the Chef class from your chef.py file. Then you must pass the Chef class in as an argument for the new Italian class.

Now the Italian chef can do everything that the average chef can do, as well as make_pasta. The Italian chef’s special dish though is still BBQ. So how can you make it so that you can have a chef make a different special dish?

If you look below, we have a Chinese chef. This chef can do everything an average chef can do as well as make_fried_rice. However if you notice, they have their own make_special_dish function. This allows the Chinese chef to have their own special dish that isn’t BBQ.

As stated before, using inheritance is a very handy way to save time and become more efficient when coding.

--

--

--

Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Exploring the Flexibility of Kubernetes

Keitaro releases new SAML2 plugin for CKAN

Establishing project management in an agile environment — one step back and two steps forward

Oracle: How to Mask Sensitive Data with the Views — Quickly and Free

uShiba Weekly, Volume 1

How I got through my first Flatiron School project — The Series

#Birdchain Exchange Listing update.

3 Ways to View AR With 3 Different Triggers (Tutorial)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thomas Muscarello

Thomas Muscarello

Software Engineer

More from Medium

Python Programs to Check Whether a Number is an Armstrong Number or Not

Python Basics: 03 introduction variable

Calculate a Number Factorial in Python — pythonpip.com

What are environment variables and how to manage them in Python?