[Python-bugs-list] [ python-Bugs-460020 ] bug or feature: unicode() and subclasses

noreply@sourceforge.net noreply@sourceforge.net
Mon, 10 Sep 2001 13:45:28 -0700


Bugs item #460020, was opened at 2001-09-09 08:41
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=460020&group_id=5470

Category: Type/class unification
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Walter Dörwald (doerwalter)
>Assigned to: Tim Peters (tim_one)
Summary: bug or feature: unicode() and subclasses

Initial Comment:
The unicode constructor returns the object passed in, 
when an instance of a subclass of unicode is passed in:
--
class U(unicode):
   pass

u1 = U(u"foo")
print type(u1)
u2 = unicode(u1)
print type(u2) 
--
this gives
--
<type '__main__.U'>
<type '__main__.U'>
--
instead of
--
<type '__main__.U'>
<type 'unicode'>
--
as it probably should be (The unicode constructor 
should construct unicode objects). With the current 
behaviour it is nearly impossible to construct a 
unicode object with the value of an instance of a 
unicode subclass, because most methods are optimized 
to return the original object if possible, e.g.
--
print type(unicode.__getslice__(u1, 0, 3))
print type(unicode.__getslice__(u1, 0, 2))
--
gives
--
<type '__main__.U'>
<type 'unicode'>
--
This should be made consistent, so that either a 
unicode object is always returned, or all methods use 
a "virtual constructor", i.e. create an object of the 
type passed in. This would simplify deriving classes 
from unicode as far fewer methods have to be 
overwritten.

But first of all the constructor should be fixed, so 
that the argument is returned unmodified only when it 
is an instance of unicode and not of a unicode 
subclass.


----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2001-09-10 13:45

Message:
Logged In: YES 
user_id=31435

Reassigned to me.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-09-10 07:48

Message:
Logged In: YES 
user_id=6380

Good catch! Other types also suffer from this, e.g. int.

added to my to-do list.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=460020&group_id=5470