Message6461
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.
|
|
Date |
User |
Action |
Args |
2007-08-23 13:56:18 | admin | link | issue460020 messages |
2007-08-23 13:56:18 | admin | create | |
|