Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32739

Re: __unicode__() works, unicode() blows up. (Never mind!)

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'builtin': 0.07; 'interpreted': 0.07; 'valueerror:': 0.07; 'works.': 0.07; 'be:': 0.09; 'buffer,': 0.09; 'here?': 0.09; 'normally,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'typeerror:': 0.09; 'def': 0.10; ':-)': 0.13; 'applies': 0.15; 'button?': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'roy': 0.16; 'subject:unicode': 0.16; 'string': 0.17; 'wrote:': 0.17; 'fix': 0.17; 'integer': 0.17; 'string,': 0.17; 'unicode': 0.17; 'jan': 0.18; 'obviously': 0.18; '>>>': 0.18; 'supposed': 0.21; '"",': 0.22; 'latter': 0.22; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; '(most': 0.27; 'am,': 0.27; 'functions.': 0.27; 'object,': 0.27; 'header:X-Complaints-To:1': 0.28; 'noticed': 0.28; 'post': 0.28; 'skip:_ 10': 0.29; 'class': 0.29; 'that.': 0.30; 'url:python': 0.32; 'file': 0.32; 'not.': 0.32; 'print': 0.32; 'traceback': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'problem,': 0.35; 'received:org': 0.36; 'skip:u 20': 0.36; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'should': 0.36; 'two': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'behind': 0.38; 'object': 0.38; 'some': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'matter': 0.61; 'subject:, ': 0.61; 'moments': 0.65; 'sit': 0.65; 'smith': 0.71; 'special': 0.73; 'article': 0.78; 'received:fios.verizon.net': 0.84; 'subject:!)': 0.84; 'url:functions': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: __unicode__() works, unicode() blows up. (Never mind!)
Date Sun, 04 Nov 2012 13:10:55 -0500
References <roy-90D9A2.08321804112012@news.panix.com> <roy-30BA92.08410804112012@news.panix.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0
In-Reply-To <roy-30BA92.08410804112012@news.panix.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3261.1352052676.27098.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352052676 news.xs4all.nl 6978 [2001:888:2000:d::a6]:57423
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32739

Show key headers only | View raw


On 11/4/2012 8:41 AM, Roy Smith wrote:
> In article <roy-90D9A2.08321804112012@news.panix.com>,
>   Roy Smith <roy@panix.com> wrote:
>
>>>>> print u.__unicode__()
>> None
>>
>>>>> print unicode(u)
>> Traceback (most recent call last):
>>    File "<stdin>", line 1, in <module>
>> TypeError: coercing to Unicode: need string or buffer, NoneType found
>>
>> What's going on here?  I thought
>> (http://docs.python.org/2/library/functions.html#unicode) the latter two
>> calls should be identical, but obviously they're not.
>
> Why is it, that no matter how long you stare at a problem, the answer
> comes to you moments after you hit the Post button? :-)
>
> The problem is that __unicode__() is supposed to return a Unicode
> object, and unicode() enforces that.  The fix is to change:
>
>      def __unicode__(self):
>          return self.username
>
> to be:
>
>      def __unicode__(self):
>          return unicode(self.username)
>
> This never got noticed before because normally, self.username already is
> a unicode string, so it just works.

The same principle applies to some of the other special methods that sit 
behind builtin functions.

 >>> class C:
	def __len__(self): return '42'  # whoops
	
 >>> len(C())
Traceback (most recent call last):
   File "<pyshell#9>", line 1, in <module>
     len(C())
TypeError: 'str' object cannot be interpreted as an integer

 >>> class C:
	def __len__(self): return -42  # whoops again
	
 >>> len(C())
Traceback (most recent call last):
   File "<pyshell#12>", line 1, in <module>
     len(C())
ValueError: __len__() should return >= 0


-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

__unicode__() works, unicode() blows up. Roy Smith <roy@panix.com> - 2012-11-04 08:32 -0500
  Re: __unicode__() works, unicode() blows up. (Never mind!) Roy Smith <roy@panix.com> - 2012-11-04 08:41 -0500
    Re: __unicode__() works, unicode() blows up. (Never mind!) aahz@pythoncraft.com (Aahz) - 2012-11-04 09:13 -0800
    Re: __unicode__() works, unicode() blows up. (Never mind!) Terry Reedy <tjreedy@udel.edu> - 2012-11-04 13:10 -0500

csiph-web