良好实践-unicode(自我)

2024-05-15 09:17:13 发布

您现在位置:Python中文网/ 问答频道 /正文

解决这个简单问题的好方法是什么?在

def __unicode__(self):
    return unicode(self.typePlace + " " + self.name)

TypeError:不支持+的操作数类型:“TipoLugar”和“str”


Tags: 方法nameself类型returndefunicodestr
2条回答

使用字符串格式而不是组合,这样既更有效,也可以为您的元素添加字符串:

return u"%s %s" % (self.typePlace, self.name)

假设typePlace本身就是一个具有自己的__str__()和/或__unicode__()函数的对象(如果它不是,并且它是一个自定义类,那么您应该添加这些方法)。因此,请在使用前将typePlace转换为unicode字符串:

return unicode(unicode(self.typePlace) + " " + self.name)

相关问题 更多 >