python - Instantiate children class from parent without changing parent code -
first of all, here (pseudo) code:
somemodule.py:
class parentclass(object): def __init__(self): if(not prevent_infinite_reursion) #just make shure not problem ;) self.somefunction() def somefunction(): # ... deep down in function ... # want "monkey patch" call constructor of childclass, # not parentclass parentclass()
othermodule.py
from somemodule import parentclass class childclass(parentclass): def __init__(self): # ... preprocessing ... super(childclass, self).__init__()
the problem is, want monkey patch parent class, call constructor of childclass, without changing code of somemodule.py. doesn't matter if it's patched in class instance(that's shure better) or globally.
i know can override somefunction, contains many lines of code, being sane.
thanks!
you use mock.patch this:
class childclass(parentclass): def somefunction(self): patch('somemodule.parentclass', childclass): super(childclass, self).somefunction()
Comments
Post a Comment