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.

Author rhettinger
Recipients kristjan.jonsson, rhettinger
Date 2009-06-23.16:57:07
SpamBayes Score 2.8441702e-08
Marked as misclassified No
Message-id <1245776229.73.0.772918114531.issue6326@psf.upfronthosting.co.za>
In-reply-to
Content
> 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.
History
Date User Action Args
2009-06-23 16:57:09rhettingersetrecipients: + rhettinger, kristjan.jonsson
2009-06-23 16:57:09rhettingersetmessageid: <1245776229.73.0.772918114531.issue6326@psf.upfronthosting.co.za>
2009-06-23 16:57:08rhettingerlinkissue6326 messages
2009-06-23 16:57:07rhettingercreate