Adds a “category” capability to Traits-based classes, similar to that provided by the Cocoa (Objective-C) environment for the Macintosh.
You can use categories to extend an existing HasTraits class, as an alternative to subclassing. An advantage of categories over subclassing is that you can access the added members on instances of the original class, without having to change them to instances of a subclass. Unlike subclassing, categories do not allow overriding trait attributes.
MetaCategoryObject(cls, class_name, bases, class_dict, is_category)¶
Used for defining “category” extensions to existing classes.
To define a class as a category, specify “Category,” followed by the name of the base class name in the base class list.
The following example demonstrates defining a category:
from traits.api import HasTraits, Str, Category class Base(HasTraits): x = Str("Base x") y = Str("Base y") class BaseExtra(Category, Base): z = Str("BaseExtra z")