Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is decorators in python example of monkey patching technique?

Recently I was reading about monkey patching technique and wondered if it can be said.

like image 211
Kishan Mehta Avatar asked May 09 '26 09:05

Kishan Mehta


2 Answers

Decorator = a function that takes a function as an argument and returns a function

Monkey patching = replacing a value of a field on an object with a different value (not necessarly functions)

In case of functions monkey patching can be performed via a decorator. So I guess decorator might be thought as an example of monkey patching. However commonly monkey patching refers to altering behaviour of a 3rd party library. In that case decorators are less useful.

like image 183
freakish Avatar answered May 10 '26 22:05

freakish


I suppose on some grammatical level they may be equivalent. However, decorators are applied at the time a function or class is defined, and monkeypatching modifies an object at runtime, making them very different both in spirit and in actual use.

like image 42
Bryan Oakley Avatar answered May 11 '26 00:05

Bryan Oakley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!