[Nipy-devel] readonly traits

Jonathan Taylor jonathan.taylor at stanford.edu
Mon May 22 14:16:46 CDT 2006


i don't understand why we think traits has no readonly traits: a more 
accurate complaint is that there is no way to typecheck ReadOnly traits 
(i.e. to make a ReadOnlyFloat trait). but this can be accomplished with 
about 20-30
lines of code (including blanklines :)).

---------------------------------

 >>> import enthought.traits as traits
 >>> class Test(traits.HasTraits):
...     x=traits.ReadOnly
...     y=traits.Float
...
 >>> a=Test(x='a string', y=3)
 >>> print a.x, a.y
a string 3.0
 >>> a.x = 4
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
enthought.traits.trait_errors.TraitError: Cannot modify the read only 
'x' attribute of a 'Test' object.
 >>> a.y = 5.
 >>>
 >>> b = Test(x=3, y=4)
 >>> b.x
3
 >>> b.x = 4
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
enthought.traits.trait_errors.TraitError: Cannot modify the read only 
'x' attribute of a 'Test' object.
 >>>
----------------------------------

my reading of the ReadOnly documentation is that these traits are 
usually set by a method, not by the user. hence, the burden lies on the 
writer of the method to ensure that its type is correct when it is set.

-- jonathan

-- 
------------------------------------------------------------------------
I'm part of the Team in Training: please support our efforts for the
Leukemia and Lymphoma Society!

http://www.active.com/donate/tntsvmb/tntsvmbJTaylor

GO TEAM !!!

------------------------------------------------------------------------
Jonathan Taylor                           Tel:   650.723.9230
Dept. of Statistics                       Fax:   650.725.8977
Sequoia Hall, 137                         www-stat.stanford.edu/~jtaylo
390 Serra Mall
Stanford, CA 94305




More information about the Nipy-devel mailing list