super() considered a case for dependency injection

submited by
Style Pass
2024-04-19 12:30:08

In following of python super() considered harmfull and __super()__ considered __super()__ by Raymond Hettinger Blog Video I came to the idea to use it for dependency injection.

If you haven't seen and used __super()__ before, see the video from Hettinger or the docs.python . In short returns a object that delegates method calls to a parent or sibling of the same type.

For those unaware of the term Dependency Injection or DI for short. It is a pattern to to decouple dependencies (it can be a database, file, class or function) from a certain implementation. So it makes it easier to test or to change later on.

For a small project (<150 hrs) I was working on, I wanted to achieve the following - Write production code (obviously) - Write Tests (Unit and Integration) - Unit Tests should be independent from production - Writing tests should be easy - No clunky adjustment to production code in order to make the testing easy 1 - Unittest should be fast - Tests should be independent of production data

An insight I got from reading a book about testing2. Is that code is a liability. I want to minimize production code. (Not in a strict sense that fewer lines are better) But the more code the more possibilities in can fail.

Leave a Comment