3 ms·
> Sure but then I have to write my real library code using an abstraction, making my code more difficult to read and maintain; It depends, right? If you sudden
by EdSchouten 7y ago
> Sure but then I have to write my real library code using an abstraction, making my code more difficult to read and maintain;
It depends, right? If you suddenly wanted to let your existing set of classes read their inputs not from disk, but from some other kind of storage (e.g., files embedded in a Zip file), you'd only need to write one extra class and you're good to go. That would be a lot harder if your code called os.* directly.
> a dependency injection system is then necessary in order to have the correct concrete implementations set up at runtime.
If by dependency injection system you mean invoking one extra constructor in, say, main() and pass the object along as a handle, sure.
- zzzeek 7y agowell again, I grew up on GOF programming and once I grokked how mocks in Python worked, I was very glad to embrace their approach, which has allowed me to write much simpler code that is more thoroughly tested; I of course still use abstractions to a great degree, but I no longer have to build out an abstraction system when I just want to make sure some fairly straightforward code is fully tested. I no longer have to build out everything as an abstraction when such a system is generally YAGNI, I can use the Python standard library directly. This allows me to do less work, write and maintain less code, and have better test coverage. It allows my code to be more fully tested even when it has not yet been abstracted, if that's what's in store for it. Patching local imports within the scope of two lines of code is a non-issue thanks to Python context managers. I have much more complicated examples of code that was already plenty complicated and mocks allowed me to get it tested quickly and effectively, instead of having to break it out into even more complexity. Basically mocks have been all productivity and no downside for me whatsoever, using the Python standard library mock which is extremely well designed.