traits.trait_type Module¶
Defines the TraitType class.
The TraitType class a trait handler that is the base class for modern
traits, and provides a richer API than the old-style traits derived from
TraitHandler.
Classes¶
- traits.trait_type.NoDefaultSpecified = <traits.trait_type._NoDefaultSpecifiedType object>¶
Singleton object that can be passed for the
default_valueargument in theTraitTypeconstructor, to indicate that no default value was specified.
- class traits.trait_type.TraitType(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶
Base class for new trait types.
This class enables you to define new traits using a class-based approach, instead of by calling the Trait() factory function with an instance of a TraitHandler derived object.
When subclassing this class, you can implement one or more of the method signatures below. Note that these methods are defined only as comments, because the absence of method definitions in the subclass definition implicitly provides information about how the trait should operate.
The optional methods are as follows:
get(self, object, name)This is the getter method of a trait that behaves like a property.
If neither this method nor the
set()method is defined, the value of the trait is handled like a normal object attribute. If this method is not defined, but theset()method is defined, the trait behaves like a write-only property. This method should return the value of thenameproperty for theobjectobject.- Parameters
- objectany
The object that the property applies to.
- namestr
The name of the property on
object.
set(self, object, name, value)This is the setter method of a trait that behaves like a property.
If neither this method nor the
get()method is implemented, the trait behaves like a normal trait attribute. If this method is not defined, but theget()method is defined, the trait behaves like a read-only property. This method does not need to return a value, but it should raise aTraitErrorexception if the specifiedvalueis not valid and cannot be coerced or adapted to a valid value.- Parameters
- objectany
The object that the property applies to.
- namestr
The name of the property on
object.- valueany
The value being assigned as the value of the property.
validate(self, object, name, value)This method validates, coerces, or adapts the specified
valueas the value of thenametrait of theobjectobject. This method is called when a value is assigned to an object trait that is based on this subclass ofTraitTypeand the class does not contain a definition for either the get() or set() methods. This method must return the originalvalueor any suitably coerced or adapted value that is a legal value for the trait. Ifvalueis not a legal value for the trait, and cannot be coerced or adapted to a legal value, the method should either raise aTraitErroror call theerrormethod to raise theTraitErroron its behalf.is_valid_for(self, value)As an alternative to implementing the
validatemethod, you can instead implement theis_valid_formethod, which receives only thevaluebeing assigned. It should returnTrueif the value is valid, andFalseotherwise.value_for(self, value)As another alternative to implementing the
validatemethod, you can instead implement thevalue_formethod, which receives only thevaluebeing assigned. It should return the validated form ofvalueif it is valid, or raise aTraitErrorif the value is not valid.post_setattr(self, object, name, value)This method allows the trait to do additional processing after
valuehas been successfully assigned to thenametrait of theobjectobject. For most traits there is no additional processing that needs to be done, and this method need not be defined. It is normally used for creating “shadow” (i.e., “mapped” traits), but other uses may arise as well. This method does not need to return a value, and should normally not raise any exceptions.- metadata = {}¶
The metadata for the trait.
- default_value = <undefined>¶
The default value for the trait type.
- get_default_value()[source]¶
Get information about the default value.
The default implementation analyzes the value of the trait’s
default_valueattribute and determines an appropriatedefault_value_typefor thedefault_value. If you need to override this method to provide a different result tuple, the following values are valid values fordefault_value_type:0, 1: The
default_valueitem of the tuple is the default value.2: The object containing the trait is the default value.
3: A new copy of the list specified by
default_valueis the default value.4: A new copy of the dictionary specified by
default_valueis the default value.5: A new instance of TraitListObject constructed using the
default_valuelist is the default value.6: A new instance of TraitDictObject constructed using the
default_valuedictionary is the default value.7:
default_valueis a tuple of the form:(callable, args, kw), wherecallableis a callable,argsis a tuple, andkwis either a dictionary or None. The default value is the result obtained by invokingcallable(\*args, \*\*kw).8:
default_valueis a callable. The default value is the result obtained by invokingdefault_value(object), whereobjectis the object containing the trait. If the trait has avalidate()method, thevalidate()method is also called to validate the result.9: A new instance of
TraitSetObjectconstructed using thedefault_valueset is the default value.
- Returns:
default_value_type, default_value – The default value information, consisting of an integer, giving the type of default value, and the corresponding default value as described above.
- Return type:
int, any
- clone(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)[source]¶
Copy, optionally modifying default value and metadata.
Clones the contents of this object into a new instance of the same class, and then modifies the cloned copy using the specified
default_valueandmetadata. Returns the cloned object as the result.Note that subclasses can change the signature of this method if needed, but should always call the ‘super’ method if possible.