Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32731 > unrolled thread
| Started by | Roy Smith <roy@panix.com> |
|---|---|
| First post | 2012-11-04 08:32 -0500 |
| Last post | 2012-11-04 13:10 -0500 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
__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
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-11-04 08:32 -0500 |
| Subject | __unicode__() works, unicode() blows up. |
| Message-ID | <roy-90D9A2.08321804112012@news.panix.com> |
Environment:
Python-2.7.3
Ubuntu Precise
mongoengine 0.6.20
I have a class which includes a __unicode__() method:
class User(mongoengine.Document):
def __unicode__(self):
return self.username
If I create an instance of this class by calling the constructor
directly, self.username is None. When I pass that to unicode(), it
blows up. However, calling __unicode__() directly, works as expected:
>>> u = User()
>>> print u.username
None
>>> 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.
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-11-04 08:41 -0500 |
| Subject | Re: __unicode__() works, unicode() blows up. (Never mind!) |
| Message-ID | <roy-30BA92.08410804112012@news.panix.com> |
| In reply to | #32731 |
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.
[toc] | [prev] | [next] | [standalone]
| From | aahz@pythoncraft.com (Aahz) |
|---|---|
| Date | 2012-11-04 09:13 -0800 |
| Subject | Re: __unicode__() works, unicode() blows up. (Never mind!) |
| Message-ID | <k767ob$8a5$1@panix5.panix.com> |
| In reply to | #32732 |
In article <roy-30BA92.08410804112012@news.panix.com>, Roy Smith <roy@panix.com> 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. You apparently need more coffee when programming after waking up! (Or even worse, staying up all night.) -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/ "....Normal is what cuts off your sixth finger and your tail..." --Siobhan
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-11-04 13:10 -0500 |
| Subject | Re: __unicode__() works, unicode() blows up. (Never mind!) |
| Message-ID | <mailman.3261.1352052676.27098.python-list@python.org> |
| In reply to | #32732 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web