This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: type(self) not preserved on some methods
Type: Stage:
Components: Interpreter Core Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: tim.peters Nosy List: average, tim.peters
Priority: normal Keywords:

Created on 2001-10-08 01:20 by average, last changed 2022-04-10 16:04 by admin. This issue is now closed.

Messages (2)
msg6863 - (view) Author: Mark J (average) Date: 2001-10-08 01:20
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


msg6864 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2001-10-08 04:04
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.
History
Date User Action Args
2022-04-10 16:04:30adminsetgithub: 35295
2001-10-08 01:20:52averagecreate