new-style classes, mutable __bases__

Jp exarkun at meson.dyndns.org
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__:
         newBases.append(latestClass(base))
     newClass.__bases__ = tuple(newBases)
     return newClass

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

class Foo(object):
   pass

updateInstance(Foo())

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?

Thanks,
   Jp




More information about the Python-list mailing list