Here is an interactive Python session which uses components of the Enthought Tool Suite (ETS):
>>> import sys
>>> sys.version
'2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)]'
>>> import traits
>>> traits.__version__
'4.5.0'
>>> from traits.api import HasTraits, Range
>>> class Foo(HasTraits):
... bar = Range (low=1, high=10)
...
>>> foo = Foo()
>>> foo.bar
1
>>> foo.bar._low
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
AttributeError: 'int' object has no attribute '_low'
I would like to be able to access the predefined limits on the bar
attribute of a Foo
instance. How can this be done?
Thanks!
The standard way to do this would be to use low
and high
traits and assign them as the limits of the Range
from traits.api import HasTraits, Range, Int
class Foo(HasTraits):
high = Int(10)
low = Int(1)
bar = Range(high='high', low='low')
You can assign the traits dynamically:
>>> f = Foo()
>>> f.bar = 5
>>> f.bar
5
>>> f.bar = 30
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/tim/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/traits/trait_types.py", line 1785, in _set
self.error( object, name, value )
File "/Users/tim/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/traits/trait_handlers.py", line 172, in error value )
traits.trait_errors.TraitError: The 'bar' trait of a Foo instance must be 1 <= a number <= 10, but a value of 30 <type 'int'> was specified.
>>> f.high = 35
>>> f.bar = 30
>>> f.bar
30