To override a public property on a subclass in Python, you can simply define the property with the same name in the subclass and implement it as desired. Let's take an example to demonstrate how it works:
main.py434 chars17 lines
In the above code, we have defined a Person
class with a public property description
which returns a string describing the person. We have then defined a Student
class which is a subclass of Person
and overrides the description
property with a new implementation that includes the student id in the description.
We can use these classes as follows:
main.py216 chars6 lines
As you can see, the Student
class overrides the description
property defined in Person
with its own implementation. Now, when we call description
on an instance of Student
, we get the student-specific description instead of the generic person description.
gistlibby LogSnag