# keywords.py --- Example of trait keywords
from
enthought.traits.api import HasTraits, Str
class
Person(HasTraits):
first_name = Str('',
desc='first or personal name',
label='First Name')
last_name = Str('',
desc='last or family name',
label='Last Name')
In this example, in a user interface editor for a Person
object, the labels “First Name” and “Last Name” would be used for entry fields
corresponding to the first_name and last_name trait attributes. If the user interface
editor supports rollover tips, then the first_name
field would display “first or personal name” when the user moves the mouse over
it; the last_name field would display “last
or family name” when moused over.
To get the value of a trait metadata attribute, you can
use the trait() method on a HasTraits object to get a reference to a specific
trait, and then access the metadata attribute:
# metadata.py --- Example of accessing trait metadata attributes
from enthought.traits.api import HasTraits, Int, List, Float, \
Instance, Any, TraitType
class Foo( HasTraits ): pass
class Test(
HasTraits ):
i =
Int(99)
lf =
List(Float)
foo =
Instance( Foo, () )
any =
Any( [1, 2, 3 ] )
t = Test()
print
t.trait( 'i' ).default # 99
print t.trait(
'i' ).default_kind # value
print
t.trait( 'i' ).inner_traits # ()
print
t.trait( 'i' ).is_trait_type( Int ) # True
print
t.trait( 'i' ).is_trait_type( Float ) # False
print
t.trait( 'lf' ).default # []
print
t.trait( 'lf' ).default_kind # list
print
t.trait( 'lf' ).inner_traits
#
(<enthought.traits.traits.CTrait object at 0x01B24138>,)
print
t.trait( 'lf' ).is_trait_type( List ) # True
print t.trait(
'lf' ).is_trait_type( TraitType ) # True
print
t.trait( 'lf' ).is_trait_type( Float ) # False
print
t.trait( 'lf' ).inner_traits[0].is_trait_type( Float )
# True
print
t.trait( 'foo' ).default # <undefined>
print
t.trait( 'foo' ).default_kind # factory
print
t.trait( 'foo' ).inner_traits # ()
print
t.trait( 'foo' ).is_trait_type( Instance ) # True
print
t.trait( 'foo' ).is_trait_type( List ) # False
print
t.trait( 'any' ).default # [1, 2, 3]
print
t.trait( 'any' ).default_kind # list
print
t.trait( 'any' ).inner_traits # ()
print
t.trait( 'any' ).is_trait_type( Any ) # True
print
t.trait( 'any' ).is_trait_type( List ) # False