John J Lee wrote: >> And once the result has been promoted to unicode, __unicode__ is used >> directly: >> >>>>> print repr("%s%s" % (a(), a())) >> __str__ >> accessing <__main__.a object at 0x00AF66F0>.__unicode__ >> __str__ >> accessing <__main__.a object at 0x00AF6390>.__unicode__ >> __str__ >> u'hihi' > > I don't understand this part. Why is __unicode__ called? Your example > doesn't appear to show this happening "once [i.e., because?] the result > has been promoted to unicode" -- if that were true, it would "stand to > reason" <wink> that the interpreter would then conclude it should call > __unicode__ for all remaining %s, and not bother with __str__.
It does try to call unicode directly, but because the example object doesn't supply __unicode__ it ends up falling back to __str__ instead. The behaviour is clearer when the example object provides both methods: >>> # Example (2.5b3) ... class a(object): ... def __str__(self): ... print "running __str__" ... return u'hi' ... def __unicode__(self): ... print "running __unicode__" ... return u'hi' ... >>> print repr("%s%s" % (a(), a())) running __str__ running __unicode__ running __unicode__ u'hihi' Cheers, Nick. -- Nick Coghlan | [EMAIL PROTECTED] | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com