
PYTHON — Overriding Subclass Properties in Python
Technology alone is not enough. It’s technology married with the liberal arts, married with the humanities, that yields the results that make our hearts sing. — Steve Jobs
Insights in this article were refined using prompt engineering methods.

PYTHON — History of Python Packaging
# Overriding Subclass Properties in Python
When you create Python classes that include properties and release them in a package or library, you should expect that your users will do a wide variety of things with them. One of these things could be subclassing them to customize their functionalities.
Let’s consider a scenario where you have an Employee class to manage employee information in your company’s internal accounting system. You already have a class called Person, and you think about subclassing it to reuse its functionality. The Person class has a .name attribute implemented as a property. However, the current implementation of .name doesn’t meet the requirement of returning the name in uppercase letters.
To solve this issue, you can override .name in the Employee class to make sure that when you access the attribute, you get the employee name in uppercase.
class Person:
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
class Employee(Person):
@property
def name(self):
return super().name.upper()
@name.setter
def name(self, value):
self._name = valueIn the above example, the Employee class overrides the getter method of .name to return the name in uppercase while retaining the setter method from the Person class.
However, when you override an existing property from a parent class, you override the whole functionality of that property. In this example, you reimplemented the getter method only, which caused the .name property to lose the setter method from the base class.
To avoid losing the functionality provided by the base class, you should provide all of the functionality you need in the new version of the property at hand.
class Employee(Person):
@property
def name(self):
return super().name.upper()
@name.setter
def name(self, value):
self._name = value
@name.deleter
def name(self):
del self._nameIn the above example, the Employee class now includes a deleter method for the .name property, ensuring that all functionality from the base class is retained while adding the necessary customization.
By following this approach, you can override properties in subclasses without losing the functionality provided by the base class.
Hope you find this tutorial helpful! Happy coding!







