擅长:python、mysql、java
<p>这样做的标准方法是使用<code>low</code>和<code>high</code>特征,并将它们指定为<code>Range</code>的界限</p>
<pre><code>from traits.api import HasTraits, Range, Int
class Foo(HasTraits):
high = Int(10)
low = Int(1)
bar = Range(high='high', low='low')
</code></pre>
<p>您可以动态分配特性:</p>
<pre><code>>>> 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
</code></pre>