Explaining Inheritance in Python Property Getters with the use of super()

The aim of this page📝 is to describe how python properties — getters — work with inheritance.

Pavol Kutaj
1 min readJan 26, 2023

If interested, see also the previous installments on properties in Python in:

We are working with shipping containers and you want to retrieve their volume

  • rule#1: containers always have a constant width and height
  • rule#2: containers differ in length
  • ⟹ width and height can be handled as class attributes
  • ⟹ length is an instance attribute, as that varies between individual containers

We also have a special type of container — a refrigerated container — where its refrigerator takes 100 ft3 of extra space that has to be subtracted from the total

  • initially, we can just override the property in the child class

… override by duplication, however, is a bad practice … in other words, we should never define important rules more than once by the same code

The solution is to use super() to retrieve the base class property and modify by subtracting the damn refrigerator’s volume

--

--

Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com