override a static property on a subclass in python

You can override a static property on a subclass in python by defining the property in the subclass with the same name as in the parent class. This will override the static property of the parent class for the subclass.

Here is an example:

main.py
class Parent:
    my_static_prop = 'parent_value'

class Child(Parent):
    my_static_prop = 'child_value'

print(Parent.my_static_prop) # Output: parent_value
print(Child.my_static_prop) # Output: child_value
210 chars
9 lines

In this example, we have a Parent class which defines a static property named my_static_prop. The Child class inherits from the Parent class and overrides the my_static_prop property with a new value.

When we print the value of my_static_prop on each class, we can see that the value is different for Parent and Child.

By defining a property with the same name in the subclass, Python will look for that property first and use it instead of the property in the parent class.

gistlibby LogSnag