Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

If you are writing code that adds or removes properties at runtime. I.e., when you want to write shoddy code.


I have written code like that in the past, and would consider doing so again in the future. When, it's appropriate, it can be hugely beneficial.

When I last did it, I was wrapping a C++ API that I needed to compare against another dataset for a merge. The C++ API (which, admittedly, I also wrote), didn't have equality or hash operators defined on the Python objects. So, I monkey-patched them in for the keys I needed. It was actually the most elegant solution I could come up with as I could then naturally use the objects in sets and easily get differences to update the appropriate dataset.

As an aside, when I wrote the python wrapper around my C++ API, I purposely didn't define equals and hash operators for the objects, despite having full information to do so on the natural keys, because I wanted the flexibility to do the monkey-patching and override how the objects were compared depending upon circumstance.


Pretty sure you can still monkeypatch the class instead of each instance, but I’m too jet lagged to think it through at the moment.


Why not competition with forwarding a.k.a a thin wrapper with the extra comparison method?


"when you want to write shoddy code" - This so much!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: