Traits User Manual
Previous: 4 Deferring Trait Definitions Table of Contents Next: 4.2 PrototypedFrom
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

4.1       DelegatesTo

The signature of the DelegatesTo initializer is:

def __init__(self, delegate, prefix='', listenable=True, **metadata)

The delegate parameter is a string that specifies the name of an attribute on the same object, which refers to the object whose attribute is deferred to; it is usually an Instance trait. The value of the delegating attribute changes whenever:

·    The value of the appropriate attribute on the delegate object changes.

·    The object referenced by the trait named in the  delegate parameter changes.

·    The delegating attribute is explicitly changed.

Changes to the delegating attribute are propagated to the delegate object’s attribute.

The prefix and listenable parameters to the initializer function specify additional information about how to do the delegation.

If prefix is the empty string or omitted, the delegation is to an attribute of the delegate object with the same name as the trait defined by the DelegatesTo object. Consider the following example:

# delegate.py --- Example of trait delegation

from enthought.traits.api \

    import DelegatesTo, HasTraits, Instance, Str

 

class Parent(HasTraits):

    first_name = Str

    last_name  = Str

 

class Child(HasTraits):

    first_name = Str

    last_name  = DelegatesTo('father')

    father     = Instance(Parent)

    mother     = Instance(Parent)

"""

>>> tony  = Parent(first_name='Anthony', last_name='Jones')

>>> alice = Parent(first_name='Alice', last_name='Smith')

>>> sally = Child( first_name='Sally', father=tony, mother=alice)

>>> print sally.last_name

Jones

>>> sally.last_name = 'Cooper' # Updates delegatee

>>> print tony.last_name

Cooper

>>> sally.last_name = sally.mother # ERR: string expected

Traceback (most recent call last):

  File "<stdin>", line 1, in ?

  File "c:\src\trunk\enthought\traits\trait_handlers.py", line

163, in error

    raise TraitError, ( object, name, self.info(), value )

enthought.traits.trait_errors.TraitError: The 'last_name' trait of a Child instance must be a value of type 'str', but a value of <__main__.Parent object at 0x009DD6F0> was specified.

"""

A Child object delegates its last_name attribute value to its father object’s last_name attribute. Because the prefix parameter was not specified in the Delegate initializer, the attribute name on the delegatee is the same as the original attribute name. Thus, the last_name of a Child is the same as the last_name of its father. When either the last_name of the Child or the last_name of the father is changed, both attributes reflect the new value.

Previous: 4 Deferring Trait Definitions Table of Contents Next: 4.2 PrototypedFrom
Traits User Manual