Swapping superclass from a module
Terry Reedy
tjreedy at udel.edu
Sun May 17 16:18:52 EDT 2009
Peter Otten wrote:
> Terry Reedy wrote:
>
>> If the names of superclasses is resolved when classes are instantiated,
>> the patching is easy. If, as I would suspect, the names are resolved
>> when the classes are created, before the module becomes available to the
>> importing code, then much more careful and extensive patching would be
>> required, if it is even possible. (Objects in tuples cannot be
>> replaced, and some attributes are not writable.)
>
> It may be sufficient to patch the subclasses:
I was not sure if __bases__ is writable or not. There is also __mro__
to consider.
> $ cat my_file.py
> class Super(object):
> def __str__(self):
> return "old"
>
> class Sub(Super):
> def __str__(self):
> return "Sub(%s)" % super(Sub, self).__str__()
>
> class Other(object):
> pass
>
> class SubSub(Sub, Other):
> def __str__(self):
> return "SubSub(%s)" % super(SubSub, self).__str__()
>
> if __name__ == "__main__":
> print Sub()
>
> $ cat main2.py
> import my_file
> OldSuper = my_file.Super
>
> class NewSuper(OldSuper):
> def __str__(self):
> return "new" + super(NewSuper, self).__str__()
>
> my_file.Super = NewSuper
> for n, v in vars(my_file).iteritems():
> if v is not NewSuper:
> try:
> bases = v.__bases__
> except AttributeError:
> pass
> else:
> if OldSuper in bases:
> print "patching", n
> v.__bases__ = tuple(NewSuper if b is OldSuper else b
> for b in bases)
>
>
> print my_file.Sub()
> print my_file.SubSub()
> $ python main2.py
> patching Sub
> Sub(newold)
> SubSub(Sub(newold))
>
> Peter
>
More information about the Python-list
mailing list