Variables are references to objects. When you reassign a variable, it doesn't affect the previous object unless its reference count goes to zero and it is deleted. In your case
class A:
a = 1
class B(A):
b = 2
B
gets the class object that happened to be assigned to A
at that moment and uses that as the parent. As long as B
uses super
for any future reference to its parent, B
never touches the A
variable again. And your reassignment of A
makes no difference.
But you did say Monkey Patch, and that is still doable. Just put stuff onto the original A class and all inheritors will see it:
import module
module.A.c = 3
assert module.B().c == 3
You can also add or replace methods
import module
def d(self):
return 4
module.A.d = d
assert module.B().d() == 4
Alternately, if you just want this change to happen for some inheritors of A, consider mixins What is a mixin, and why are they useful?