6.1.2
Overriding Default Values in a Subclass
Often, a subclass must override a trait attribute in a
parent class by providing a different default value. You can specify a new
default value without completely re-specifying the trait definition for the
attribute. For example:
# override_default.py -- Example of overriding a default value for
#
a trait attribute in a subclass
from
enthought.traits.api import HasTraits, Range, Str
class
Employee(HasTraits):
name =
Str
salary_grade = Range(value=1, low=1, high=10)
class
Manager(Employee):
salary_grade = 5
In this example, the salary_grade
of the Employee class is a range from 1 to 10, with a default value of 1. In
the Manager subclass, the default value of salary_grade
is 5, but it is still a range as defined in the Employee class.
|