Python Inheritance

Thomas Muscarello
2 min readJul 19, 2021

--

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.

--

--