On Mon, 21 Aug 2006, Nick Coghlan wrote:
> 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:
[...]
If the interpreter is falling back from __unicode__ to __str__ (rather
than the other way around, kind-of), that makes much more sense. I
understood that __unicode__ was not provided, of course -- what wasn't
clear to me was why the interpreter was calling/accessing those
methods/attributes in the sequence it does. Still not sure I understand
what the third __str__ above comes from, but until I've thought it through
again, that's my problem.
John
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com