class clas1(object):
def __init__(self,name):
self.name = name
def __unicode__(self):
return 'name' + self.name
obj1 = clas1('dzxczx')
print obj1
我是python2.7怎么用这个__unicode__没有效果啊 用__str__倒是有效果 这是为什么啊?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你可以写成
或者:
print 默认调用 __str__
unicode 默认调用 __unicode__
repr 默认调用 __repr__
先说原因,因为
print()
函数默认调用__str__
,你应该调用unicode()
参见:
Str 和 Unicode 方法
SO 上的问题
摘抄其中一段:
print交互默认使用__str__方法,你的unicode方法肯定不会调用