Sub-classing unicode: getting the unicode value
"Martin v. Löwis"
martin at v.loewis.de
Mon Dec 31 11:55:42 EST 2007
>>> How does my object get its own value?
>> def __unicode__(self):
>> return unicode(self)
>
> I get an endless recursion with this.
I see. That worked fine in Python 2.4, but give a stack overflow
in Python 2.5.
Depending on your exact class definition, something like
return super(unicode, self).__getitem__(slice(0,len(self)))
should work.
> I must admit, though, that I probably overestimate the costs
> connected with unicode(my_excerpt) because Gabriel is probably right
> that no real conversion takes place. A mere attribute lookup may
> still be cheaper, but only very slightly.
I don't understand that remark. To implement the conversion in the
way you want it to be, it definitely needs to produce a copy of
self.
Regards,
Martin
More information about the Python-list
mailing list