Dynamically mixin a base class to an instance in Python
This dynamically defines a new class GentlePerson
, and reassigns p
's class to it:
class Gentleman(object): def introduce_self(self): return "Hello, my name is %s" % self.nameclass Person(object): def __init__(self, name): self.name = namep = Person("John")p.__class__ = type('GentlePerson',(Person,Gentleman),{})print(p.introduce_self())# "Hello, my name is John"
Per your request, this modifies p
's bases, but does not alter p
's original class Person
. Thus, other instances of Person
are unaffected (and would raise an AttributeError
if introduce_self
were called).
Although it was not directly asked in the question, I'll add for googlers and curiosity seekers, that it is also possible to dynamically change a class's bases but (AFAIK) only if the class does not inherit directly from object
:
class Gentleman(object): def introduce_self(self): return "Hello, my name is %s" % self.nameclass Base(object):passclass Person(Base): def __init__(self, name): self.name = namep = Person("John")Person.__bases__=(Gentleman,object,)print(p.introduce_self())# "Hello, my name is John"q = Person("Pete")print(q.introduce_self())# Hello, my name is Pete