[issue6326] Add a "swap" method to list
Raymond Hettinger
report at bugs.python.org
Tue Jun 23 18:57:09 CEST 2009
Raymond Hettinger <rhettinger at users.sourceforge.net> added the comment:
> One application of this is to make help a performance problem when one
> wants to upgrade a list instance into a subclass instance.
Since this bypasses the subclass's __init__ and other methods, doesn't
it risk violating subclass invariants?
class CapList(list):
def __init__(self, iterable=()):
for elem in iterable:
self.append(elem.upper())
class NoneCountingList(list):
def __init__(self, iterable=()):
list.__init__(self, iterable)
self.nones = self.count(None)
def append(self, value):
list.append(self, value)
self.nones += 1 if value is None else 0
def extend(self, iterable):
for elem in iterable:
self.append(elem)
. . .
IOW, a swap() method is problematic for some subclasses because it
bypasses all of the subclass insertion/removal logic. The problem is
compounded for subclasses written as C extensions because violating the
internal invariants may lead to a crash.
----------
nosy: +rhettinger
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6326>
_______________________________________
More information about the Python-bugs-list
mailing list