[Python-bugs-list] [ python-Bugs-468887 ] type(self) not preserved on some methods
noreply@sourceforge.net
noreply@sourceforge.net
Sun, 07 Oct 2001 21:04:31 -0700
Bugs item #468887, was opened at 2001-10-07 18:20
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=468887&group_id=5470
Category: Type/class unification
>Group: Not a Bug
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Mark J (average)
>Assigned to: Tim Peters (tim_one)
Summary: type(self) not preserved on some methods
Initial Comment:
Python-2.2a4
For user-defined types derived from built-in types, it
appears that built-in methods which return copies are
not preserving the type of self.
>>> class Test(list): pass
>>> t=Test()
>>> map(type, [t, t[:], t+t])
[<class '__main__.Test'>, <type 'list'>, <type 'list'>]
>>> t += t
>>> type(t)
<class '__main__.Test'>
Same with types int, dictionary, etc.
Thanks,
Mark
----------------------------------------------------------------------
>Comment By: Tim Peters (tim_one)
Date: 2001-10-07 21:04
Message:
Logged In: YES
user_id=31435
This is deliberate. If, e.g., you don't override the
slicing operator in Test, then t[:] is handled by the base
class's slicing operator. The base class can't know what
invariants Test needs to preserve, so does the best it can
by constructing a list. If you want operators that return
Test instances instead, you have to supply them.
Note that base-class operators *sometimes* returned
instances of subclasses in earlier 2.2 alphas, but
unpredictably (depending on internal optimizations). This
was properly reported as a bug, and was fixed for 2.2a4;
see bug 460020 for details.
----------------------------------------------------------------------
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=468887&group_id=5470