Traits User Manual
Previous: 3.2.4 General Static Handler Signatures Table of Contents Next: 3.4 Undefined Object
Traits User Manual
1 Introduction
1.1 What Are Traits?
1.2 Background
2 Defining Traits: Initialization and Validation
2.1 Predefined Traits
2.1.1 Predefined Traits for Simple Types
2.1.2 Other Predefined Traits
2.2 Trait Metadata
2.2.1 Internal Metadata Attributes
2.2.2 Recognized Metadata Attributes
2.2.3 Accessing Metadata Attributes
3 Trait Notification
3.1 Dynamic Notification
3.1.1 Example of a Dynamic Notification Handler
3.1.2 The name Parameter
3.1.3 Notification Handler Signatures
3.1.4 Dynamic Handler Special Cases
3.2 Static Notification
3.2.1 Handler Decorator
3.2.2 Specially-named Notification Handlers
3.2.3 Attribute-specific Handler Signatures
3.2.4 General Static Handler Signatures
3.3 Trait Events
3.4 Undefined Object
4 Deferring Trait Definitions
4.1 DelegatesTo
4.2 PrototypedFrom
4.3 Keyword Parameters
4.3.1 Prefix Keyword
4.3.2 Listenable Keyword
4.4 Notification with Deferring
5 Custom Traits
5.1 Trait Subclassing
5.1.1 Defining a Trait Type
5.1.2 Defining a Trait Property
5.1.3 Other TraitType Members
5.2 The Trait() Factory Function
5.2.1 Trait () Parameters
5.2.2 Mapped Traits
5.3 Trait Handlers
5.3.1 TraitPrefixList
5.3.2 TraitPrefixMap
5.4 Custom Trait Handlers
5.4.1 Example Custom Trait Handler
6 Advanced Topics
6.1 Initialization and Validation Revisited
6.1.1 Dynamic Initialization
6.1.2 Overriding Default Values in a Subclass
6.1.3 Reusing Trait Definitions
6.1.4 Trait Attribute Definition Strategies
6.1.5 Type-Checked Methods
6.2 Interfaces
6.2.1 Defining an Interface
6.2.2 Implementing an Interface
6.2.3 Using Interfaces
6.3 Adaptation
6.3.1 Defining Adapters
6.3.2 Using Adapters
6.3.3 Controlling Adaptation
6.4 Property Traits
6.4.1 Property Factory Function
6.4.2 Caching a Property Value
6.5 Persistence
6.5.1 Pickling HasTraits Objects
6.5.2 Predefined Transient Traits
6.5.3 Overriding __getstate__()
6.5.4 Unpickling HasTraits Objects
6.5.5 Overriding __setstate__()
6.6 Useful Methods on HasTraits
6.6.1 add_trait()
6.6.2 clone_traits()
6.6.3 set()
6.6.4 add_class_trait()
6.7 Performance Considerations of Traits

3.3       Trait Events

The Traits package defines a special type of trait called an event. Events are instances of (subclasses of) the Event class.

There are two major differences between a normal trait and an event:

·    All notification handlers associated with an event are called whenever any value is assigned to the event. A normal trait attribute only calls its associated notification handlers when the previous value of the attribute is different from the new value being assigned to it.

·    An event does not use any storage, and in fact does not store the values assigned to it. Any value assigned to an event is reported as the new value to all associated notification handlers, and then immediately discarded. Because events do not retain a value, the old argument to a notification handler associated with an event is always the special Undefined object (see Section 3.4). Similarly, attempting to read the value of an event results in a TraitError exception, because an event has no value.

As an example of an event, consider: 

# event.py --- Example of trait event

from enthought.traits.api import Event, HasTraits, List, Tuple

 

point_2d = Tuple(0, 0)

 

class Line2D(HasTraits):

    points = List(point_2d)

    line_color = RGBAColor('black')

    updated = Event

   

    def redraw():

        pass # Not implemented for this example

 

    def _points_changed():

        self.updated = True

       

    def _updated_fired():

        self.redraw()

In support of the use of events, the Traits package understands attribute-specific notification handlers with names of the form _name_fired(), with signatures identical to the _name_changed() functions. In fact, the Traits package does not check whether the trait attributes that _name_fired() handlers are applied to are actually events. The function names are simply synonyms for programmer convenience.

Similarly, a function named on_trait_event() can be used as a synonym for on_trait_change() for dynamic notification.

Previous: 3.2.4 General Static Handler Signatures Table of Contents Next: 3.4 Undefined Object
Traits User Manual