Monkey patch against inheritance and overriding in Python

I am writing an experimental graphical shell. A GUI is created by combining components (similar to widgets). There are several “native” component classes. The framework user specializes in providing specific methods that define configuration, bindings, etc. This can be done by expanding your own class and overriding its methods, which is good, but many derived classes will be created only once. Alternatively, I could provide a factory function that will take its own class and specialized methods (functions, really). This function will instantiate its own class and replace the corresponding methods. Any reason to prefer one approach over another?

+4
source share
2 answers

The Monkey patch is not a real design pattern, more like a hack, which leads to spaghetti code due to abuse.

What you probably want as an alternative to replacing monkeys is adapters or aspect-oriented programming .

+2
source

What most GUI tools (wxpython, kivy, pyQT) in python do is an inheritance approach.

I assume that both approaches should work, but using inheritance will be more familiar to your potential users.

, , (suck kv lang html) ,

0

Source: https://habr.com/ru/post/1623671/


All Articles