Is it possible to append code to an existing class method without fully overwriting it?
Example:
class Test:
def Do(self):
print('initial')
And then add a print('new') to the Test.Do() method without overwriting the method fully. So something like:
def better_do(self):
print('new') #Could also go at the end
run_original_do(self)
test = Test()
test.Do() #This would then print new and initial
Reason: If I would update the third party library I want the possibly updated code to be updated and then I want my code to run before or after that new code. If I overwrite the entire method it means that I make that code static and any updates will not come through later unless I do the manual work of updating.
EDIT: One way of doing this I found is copying the original function and then using it in the newly defined one. This is taken from: https://stackoverflow.com/a/13503277/4532098 https://stackoverflow.com/a/6528148/4532098
import functools
import types
class Test:
def initial(self, x):
print(x)
print('initial original')
def copy_func(func):
new_func = types.FunctionType(func.__code__, func.__globals__, name=func.__name__,
argdefs=func.__defaults__,
closure=func.__closure__)
new_func = functools.update_wrapper(new_func, func)
new_func__kwdefaults__ = func.__kwdefaults__
return new_func
z = copy_func(Test.initial)
def x(self, x):
print('new!')
z(self, x)
Test.initial = x
y = Test()
y.initial('xxx')
EDIT 2: Seeing it closed as duplicate, but the main thing here that I might not have stated clearly is that this is about a third party library. The answers in the "duplicate" post are all based on changing the code of the third party library, which I cannot. The overwritten class is used outside of my code as well so subclassing and putting in a decorator would not work, it does need to be the original class.
EDIT 3: No the duplication was sort of correct indeed, the nicest answer is in: https://stackoverflow.com/a/8726680/4532098 Not in the selected answer