new-style classes, mutable __bases__

Jp exarkun at
Tue Aug 27 07:44:24 EDT 2002

These functions are part of a rebuild module to updating
code without restarting the program

def latestClass(oldClass):
     """Get the latest version of a class.
     if oldClass is object:
         return oldClass
     module = __import__(oldClass.__module__, {}, {}, 'nothing')
     newClass = getattr(module, oldClass.__name__)
     newBases = []
     for base in newClass.__bases__:
     newClass.__bases__ = tuple(newBases)
     return newClass

def updateInstance(self):
     """Updates an instance to be current
     self.__class__ = latestClass(self.__class__)

class Foo(object):


Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<stdin>", line 4, in updateInstance
   File "<stdin>", line 11, in latestClass
TypeError: readonly attribute

Old-style classes, this works great.  New-style, not so much.
Is there a workaround for this?  Is it permanent behavior?
Is there a better way to approach this problem?


More information about the Python-list mailing list