Question

Are CheckListEditors able to handle lists of objects that are not strings but implement __str__ and __repr__?

Was it helpful?

Solution

Not as such. CheckListEditor assumes that values are either strings or tuples of (element, string). It uses isinstance(basestring) on the objects to check whether they are strings, and if not it assumes they are tuples of (element,string).

In some cases it is not especially convenient to provide tuples of (element,string). Mainly this is true when the element in the list is a trait from the model object -- providing a trait of (element,name) tuples is a bit awkward. However, implementing indexing is a convenient workaround (or atrocious hack) which fools CheckListEditor into thinking that (element,name) tuples is what its getting.

class Nameable(HasTraits)

  def __repr__(self): return "Some String Representation"

  def __getitem__(self,key):
    if key==0: return self
    elif key==1: return self.__repr__()
    else: raise KeyError
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top