The Enthought Tool Suite team is pleased to announce the release of Traits 4.6 . Together with the release of TraitsUI 5.1 last year, these core packages of Enthought’s open-source rapid application development tools are now compatible with python 3 as well as Python 2.7. Long-time fans of Enthought’s open-source offerings will be happy to hear about the recent updates and modernization we’ve been working on, including the recent release ofMayavi 4.5with Python 3 support, while newcomers to Python will be pleased that there is an easy way to get started with GUI programming which grows to allow you to build applications with sophisticated, interactive 2D and 3D visualizations.
A Brief Introduction to Traits and TraitsUITraits is a mature reactive programming library for Python that allows application code to respond to changes on Python objects, greatly simplifying the logic of an application. TraitsUI is a tool for building desktop applications on top of the Qt or WxWidgets cross-platform GUI toolkits. Traits, together with TraitsUI, provides a programming model for Python that is similar in concept to modern and popular javascript frameworks like React , Vue and Angular but targeting desktop applications rather than the browser.
Traits is also the core of Enthought’s open source 2D and 3D visualization librariesChaco andMayavi, drives the internal application logic of Enthought products likeCanopy,Canopy Geoscience andVirtual Core, and Enthought’s consultants appreciate its the way it facilitates the rapid development of desktop applications for our consulting clients . It is also used by several open-source scientific software projects such as the HyperSpy multidimensional data analysis library and the pi3Diamond application for controlling diamond nitrogen-vacancy quantum physics experiments, and in commercial projects such as the PyRX Virtual Screening software for computational drug discovery.

The open-source pi3Diamond application built with Traits, TraitsUI and Chaco by Swabian Instruments .
Traits is part of the Enthought Tool Suite of open source application development packages and is available to install through Enthought Canopy’s Package Manager (you candownload Canopy here) or via Enthought’s new edm command line package and environment management tool. Running
edm install traits
at the command line will install Traits into your current environment.
TraitsThe Traits library provides a new type of Python object which has an event stream associated with each attribute (or “trait”) of the object that tracks changes to the attribute. This means that you can decouple your application model much more cleanly: rather than an object having to know all the work which might need to be done when it changes its state, instead other parts of the application register the pieces of work that each of them need when the state changes and Traits automatically takes care running that code. This results in simpler, more modular and loosely-coupled code that is easier to develop and maintain.
Traits also provides optional data validation and initialization that dramatically reduces the amount of boilerplate code that you need to write to set up objects into a working state and ensure that the state remains valid. This makes it more likely that your code is correct and does what you expect, resulting in fewer subtle bugs and more immediate and useful errors when things do go wrong.
When you consider all the things that Traits does, it would be reasonable to expect that it may have some impact on performance, but the heart of Traits is written in C and knows more about the structure of the data it is working with than general Python code. This means that it can make some optimizations that the Python interpreter can’t, the net result of which is that code written with Traits is often faster than equivalent pure Python code.
Example: A To-Do List in TraitsTo be more concrete, let’s look at writing some code to model a to-do list. For this, we are going to have a “to-do item” which represents one task and a “to-do list” which keeps track of all the tasks and which ones still need to be done.
Each “to-do item” should have a text description and a boolean flag which indicates whether or not it has been done. In standard Python you might write this something like:
class ToDoItem(object):def __init__(self, description='Something to do', completed=False):
self.description = description
self.completed = completed
But with Traits, this would look like:
from traits.api import Bool, HasTraits, Unicodeclass ToDoItem(HasTraits):
description = Unicode('Something to do')
completed = Bool
You immediately notice that Traits is declarative all we have to do is declare that the ToDoItem has attributes description and completed and Traits will set those up for us automatically with default values no need to write an __init__ method unless you want to, and you can override the defaults by passing keyword arguments to the constructor:
>>> to_do = ToDoItem(description='Something else to do')>>> print(to_do.description)
Something else to do
>>> print(to_do.completed)
False
Not only is this code simpler, but we’ve declared that the description attribute’s type is Unicode and the completed attribute’s type is Bool , which means that Traits will validate the type of new values set to these Traits:
>>> to_do.completed = 'yes'TraitError: The 'completed' trait of a ToDoItem instance must be a boolean,
but a value of 'yes' <type 'str'> was specified.
Moving on to the second class, the “to-do list” which tracks which items are completed. With standard Python classes each ToDoItem would need to know the list which it belonged to and have a special method that handles changing the completed state, which at its simplest might look something like:
class ToDoItem(object):def __init__(self, to_do_list, description='', completed=False):
self.to_do_list = to_do_list
self.description = description
self.completed = completed def