I'm looking for resources that could help me write better, more professional code in Python.<p>I've worked extensively as a software developer in Java before, so I am aware of the many things that go into writing "real" code (as opposed to what one writes for Leetcode or even an academic project), from little things like using the right annotations, to bigger ones like dependency injection frameworks.<p>I've used Python for Leetcode, scripting, isolated ML work, writing little games, etc., and I've read and practiced much of PEP8, so it's not that I write bad Python code. But I do feel that I could be doing so much better (since I've done better in Java) and I'd like to get to that level of proficiency as soon as I can.<p>In that vein, I'm looking for any resources that have worked for any of you and you think would be suited for me. Thanks!
Assuming you have the fundamentals down, I’d recommend taking the time to dig in and understand the source code of a couple well written packages. Start with some simple libraries like datetime, requests, flask and move on to more complex, modern examples like pandas / fastapi.<p>Clone the package, run the tests, break the tests and try adding functionality. You’ll learn a lot - I know I did when I was starting out.<p>I’d also recommend checking out Fluent Python.
Avoid writing java in python.<p>Don't stretch inheritance where where they are not needed - avoid factory classes unless you know for certain that it's called for.<p>Use pythonic stuff like @decorators and enjoy functions as first class objects.<p>Finally, try to avoid using an IDE. This keeps your files and folders structures simple and organized out of <i>necessity</i>. In Java it's almost impossible, but it's very possible in python as it removes so much verbosity.
Try new things. Simple over everything is the key to production python code. Too many layers of abstraction is a massive footgun, especially if youre passing objects around a lot, due to dynamic typing etc.<p>Constantly circle back and refactor. Ruthlessly.<p>Same as in any lang, focus on data structures and algorithms (business logic is algorithmic) rather than the implementation, at least to start with. Get it working (usually easy in python, it's so dynamic!), make it good, make it fast. A good data model makes coding a joy. If your data structures suck every little thing will accrue friction. This is a red flag that you chose the wrong data model.<p>Source: 1000s of python hours in engineering and research roles
RealPython.com<p>Learn all about pytest and how to use its fixtures well.<p>Mypy for use in conjunction with type annotations for static analysis.<p>Packaging: Python-poetry.org<p>Personally, I recommend Deal (design by contract) and/or Hypothesis (property-based testing) libraries, too.<p>Controversial opinion: Stay away from Flask and all of its derivations. That framework is badly designed. Learn from Django instead.
Going through Trey Hunner's Python Morsels [0] exercises is worth your time. Each exercise starts by describing desired outcomes and it's your job to make the tests pass. The accompanying screencasts are just the right length and level of detail. At the end you can compare your code to his. It covers all the right ways to write python.<p>[0] - <a href="https://www.pythonmorsels.com/" rel="nofollow noreferrer">https://www.pythonmorsels.com/</a>
Start reading production level Python.<p>I mean, so many people want to write production-level code and yet never took the time to actually read the production-level code right in their faces!<p>Even the standard library is worth seeing. Next time you import pathlib.Path, right click it, select "See Definition" and go find out how the sausage is made.<p>Obviously you are not expected to understand _everything_. But you will be surprised you will understand a bit. And then a bit more. And you will start getting comfortable dealing with production-level code. Soon you'll start writing it yourself.<p>This little habit skyrocketed my Python game
The rule of writing better python is to forget most of what you have learned writing Java. Avoid boilerplate code . Avoid deep nested classes like factory.nextfactory.nextnext factory . Avoid premature over engineering abstractions .only use class if you have to . Make it simple. When you can achieve 20 lines of easy to read python code that probably need 200 lines of Java code that you used to write , then you succeed.
Use mypy if you can, and then as much as you can. python's dynamic type system is not only a pain for debugging and for catching problems (before they arrive as cryptic runtime errors way too late in production), it also makes it hard for other developers to know how data is shaped, and what functions and methods are doing without deep inspection.<p>mypy brings some of the sanity back.
Improving your Python skills for production-level code involves practice and continuous learning. Participate in open-source projects, review high-quality code, and read Python best practices. Collaborating with experienced developers and seeking feedback can help refine your skills. Stay curious, be patient, and celebrate progress along the way!
Some off-the-cuff tips, at least --<p>TDD is very easy with Python because the unit testing framework is built in -- I'd suggest writing tests for just about everything you do.<p>Additionally, the typing system is expanding all the time. Make sure you're adding type annotations where / when you can; even lightweight ones like TypedDict help.
Not a learning source per se, but a good tool to learn "best practices" is using ruff [0] as your linter.<p>I enabled nearly all the rules it has available. And I've learned so much from it.<p>[0]: <a href="https://beta.ruff.rs" rel="nofollow noreferrer">https://beta.ruff.rs</a>
A classic: <a href="https://www.youtube.com/watch?v=wf-BqAjZb8M">https://www.youtube.com/watch?v=wf-BqAjZb8M</a> (Raymond Hettinger - Beyond PEP 8 -- Best practices for beautiful intelligible code - PyCon 2015)
Learn good style (if you aren’t given one the google style guide is pretty common)and unit test your code.
At google they call writing good code “readability”. When you get “python readability” your code is readable. Which makes it easier to understand, debug and maintain.