Listing 4. Our Final Version of Mix-in
import types
def MixIn(pyClass, mixInClass, makeAncestor=0):
if makeAncestor:
if mixInClass not in pyClass.__bases__:
pyClass.__bases__ = (mixInClass,) + pyClass.__bases__
else:
# Recursively traverse the mix-in ancestor
# classes in order to support inheritance
baseClasses = list(mixInClass.__bases__)
baseClasses.reverse()
for baseClass in baseClasses:
MixIn(pyClass, baseClass)
# Install the mix-in methods into the class
for name in dir(mixInClass):
if not name.startswith('__'):
# skip private members
member = getattr(mixInClass, name)
if type(member) is types.MethodType:
member = member.im_func
setattr(pyClass, name, member)