3 ms·
If you are a beginner and you are coding in Python, I'd say hold off on the design patterns (as defined by the gang of four) but instead focus on idiomatic Pyth
by ayhanfuat 3y ago
If you are a beginner and you are coding in Python, I'd say hold off on the design patterns (as defined by the gang of four) but instead focus on idiomatic Python. The reason is many of the design patterns are baked into the language. It may create more confusion. For idiomatic Python, I can recommend two books that I've found really helpful:
1. Python Cookbook by David Beazley (it doesn't have the latest features like pattern matching but the 3rd edition uses Python 3 and it is still very relevant)
2. Fluent Python by Luciano Ramalho (the book itself is very good but it is also very valuable for the list of suggested readings at the end of each chapter).
- swaginator 3y agoI agree with this. Read both of those books on the job during my first year. Fluent Python really takes you from "Python as a scripting language" to "Python as a functional language". An example of why certain patterns aren't necessary in Python: I have two types of repositories, one that wraps my API calls, and one that wraps an in-memory database for unit testing. A module called repos.py chooses which of these to instantiate when it is loaded based off an environment variable. Because every module in Python is loaded once, every file/class that needs a repository for data access can import from repos.py and receive an instantiated repository object. There is no need for dependency injection because the dependency can be imported from a module, and the module can be modified at runtime. This is extremely powerful too, since, so long as my in-memory repository and API repository have the same behavior, I can write a test suite that doubles as both unit tests and integration tests, all I need to do is change a single environment variable. I think Java's limitation of "one class one file" and no support for first-class functions made design patterns necessary. Then you try out a language with some functional features and you're like "oh wait, that was just a workaround for what other languages have always been able to do".