help(unicode)
打印类似:
class unicode(basestring)
| unicode(string [, encoding[, errors]]) -> object
...
但您可以使用与基本字符串不同的东西作为参数,您可以执行 unicode(1) 并获取 u'1'。该通话中发生了什么? int 没有要调用的 __unicode__ 方法。
If __unicode__
存在则调用,否则返回__str__
class A(int):
def __str__(self):
print "A.str"
return int.__str__(self)
def __unicode__(self):
print "A.unicode"
return int.__str__(self)
class B(int):
def __str__(self):
print "B.str"
return int.__str__(self)
unicode(A(1)) # prints "A.unicode"
unicode(B(1)) # prints "B.str"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)