A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2006-August/068411.html below:

[Python-Dev] String formatting / unicode 2.5 bug?

[Python-Dev] String formatting / unicode 2.5 bug? [Python-Dev] String formatting / unicode 2.5 bug?Nick Coghlan ncoghlan at gmail.com
Mon Aug 21 11:37:13 CEST 2006
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   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4