2009年9月14日月曜日

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

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

おそろしや
  1. class Surprise(object):  
  2.     def __str__(self):  
  3.         return "[str]"  
  4.     def __unicode__(self):  
  5.         return u"[unicode]"  
  6.   
  7. surprise = Surprise()  
  8.   
  9. print "%s %s %s" % (surprise, u"foo", surprise)  

出力:
[str] foo [unicode]

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


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

0 件のコメント: