2009年9月14日月曜日

Interpolation surprise — And now for something completely Pythonic...

Interpolation surprise — And now for something completely Pythonic...

おそろしや

class Surprise(object):
def __str__(self):
return "[str]"
def __unicode__(self):
return u"[unicode]"

surprise = Surprise()

print "%s %s %s" % (surprise, u"foo", surprise)

出力:
[str] foo [unicode]

つまり、途中でUnicodeが来ると、それ以降は__unicode__が呼ばれるようになるわけです。


ちなみにPython2.3なら
出力:
[str] foo [str]
となります。

0 件のコメント: