For some applications, it can be necessary to retrieve or manipulate the View objects associated with a given model object. The HasTraits class defines two methods for this purpose: trait_views() and trait_view().
The trait_views() method, when called without arguments, returns a list containing the names of all Views defined in the object’s class. For example, if sam is an object of type SimpleEmployee3 (from Example 6), the method call sam.trait_views() returns the list ['all_view', 'traits_view'].
Alternatively, a call to trait_views(view_element_type) returns a list of all named instances of class view_element_type defined in the object’s class. The possible values of view_element_type are:
Thus calling trait_views(View) is identical to calling trait_views(). Note that the call sam.trait_views(Group) returns an empty list, even though both of the Views defined in SimpleEmployee contain Groups. This is because only named elements are returned by the method.
Group and Item are both subclasses of ViewSubElement, while ViewSubElement and View are both subclasses of ViewElement. Thus, a call to trait_views(ViewSubElement) returns a list of named Items and Groups, while trait_views(ViewElement) returns a list of named Items, Groups and Views.
The trait_view() method is used for three distinct purposes:
For example: