[Python-Dev] __str__ bug?

Tamito KAJIYAMA RD6T-KJYM at asahi-net.or.jp
Tue Oct 24 21:41:27 CEST 2006


I believe you've overriden unicode.__str__ as you expect.

class S(str):
    def __str__(self): return "S.__str__"

class U(unicode):
    def __str__(self): return "U.__str__"

print str(S())
print str(U())

This script prints:

S.__str__
U.__str__

Regards,

-- 
KAJIYAMA, Tamito <RD6T-KJYM at asahi-net.or.jp>


>Is this a bug?  If not, how do I override __str__ on a unicode derived class?
>
>class S(str):
>    def __str__(self): return '__str__ overridden'
>
>class U(unicode):
>    def __str__(self): return '__str__ overridden'
>    def __unicode__(self): return u'__unicode__ overridden'
>
>s = S()
>u = U()
>
>print 's:', s
>print "str(s):", str(s)
>print 's substitued is "%s"\n' % s
>print 'u:', u
>print "str(u):", str(u)
>print 'u substitued is "%s"' % u
>
>-----------------------------------------------------
>
>s: __str__ overridden
>str(s): __str__ overridden
>s substitued is "__str__ overridden"
>
>u:
>str(u): __str__ overridden
>u substitued is ""
>
>Results are identical for 2.4.2 and 2.5c2 (running under windows).
>
>   Mike


More information about the Python-Dev mailing list