Search code examples
pythonvalidationenthoughttraits

How to restrict values in a Float trait in Enthought traits module?


I'm a bit new to Enthought traits module. I would like to specify a trait for which only a few floating-point values are allowed. Unfortunately, I can't use a Enum because it breaks the code base I'm working on. How can I specify which values are valid for a Float? Do I have to create another class deriving from Float with an overloaded validate?


Solution

  • Yes, the traits docs indicate that you should subclass the Float trait and add your custom validation code.

    See the section about Custom Traits. I'd take a look at the source for the implementation of Enum as well.