__str__()
is the old method -- it returns bytes. __unicode__()
is the new, preferred method -- it returns characters. The names are a bit confusing, but in 2.x we're stuck with them for compatibility reasons. Generally, you should put all your string formatting in __unicode__()
, and create a stub __str__()
method:
def __str__(self):
return unicode(self).encode('utf-8')
In 3.0, str
contains characters, so the same methods are named __bytes__()
and __str__()
. These behave as expected.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…