[英]Using CheckListEditor with something other than List(Str)
__str__
是否能够处理不是字符串而是实现__str__
和__repr__
的对象列表?
Not as such. 不是这样的。 CheckListEditor assumes that values are either strings or tuples of (element, string). CheckListEditor假定值是字符串或(元素,字符串)的元组。 It uses isinstance(basestring)
on the objects to check whether they are strings, and if not it assumes they are tuples of (element,string). 它在对象上使用isinstance(basestring)
来检查它们是否为字符串,如果不是,则假定它们为(element,string)的元组。
In some cases it is not especially convenient to provide tuples of (element,string). 在某些情况下,提供(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. 主要是当列表中的元素是模型对象的特征时才适用-提供(element,name)元组的特征有点尴尬。 However, implementing indexing is a convenient workaround (or atrocious hack) which fools CheckListEditor into thinking that (element,name) tuples is what its getting. 但是,实现索引是一种便捷的解决方法(或令人发指的骇客),它使CheckListEditor误以为(元素,名称)元组就是它所得到的。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.