简体   繁体   English

将CheckListEditor与List(Str)以外的其他东西一起使用

[英]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.

相关问题 使用itetuples之外的其他东西来提高速度 - Using something other than itertuples for pandas for speed 将Pelican Static Site Generator用于博客以外的其他内容 - Using Pelican Static Site Generator for something other than a blog 使用单引号或双引号以外的其他内容来设置变量? - Using something other than single or double quotes to set variable? Pandas 使用 str replace 将不在国籍列表中的国家替换为“其他” - Pandas replace countries not in list of nationalities with 'other' using str replace 返回-1索引以外的东西 - Returning something other than -1 index 列表中的ElasticSearch查询包含X并包含X以外的其他内容 - ElasticSearch query from list to contain X and contain something other than X 在 Python 中,除了使用 str.translate() 之外,替换给定字符串中某些字符的最快方法是什么? - In Python, what is the fastest way to replace certain characters in a given string other than using str.translate()? 使用str()将str添加反斜杠\\到字符串的列表 - List to str adding backslash \ to string using str() 在其他列表范围内创建的列表 - list created str in range other list 在创建对象时返回对象以外的东西 - return something other than the object on creation of object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM