6 ms·
Ask HN: How can I get better at writing production-level Python?
I'm looking for resources that could help me write better, more professional code in Python.
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.
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.
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!
- mydriasis 3y agoSome off-the-cuff tips, at least -- 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. 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.
- ducharmdev 3y agoWhen I used Python for Advent of Code a few years ago, I found that Python makes unit testing fun. Coming from .NET 6 development, I'll do what I need to do when it comes to fleshing out unit tests, but the brevity of Python unit tests was real pleasant to work with.
- nhgiang 3y agoRealPython.com Learn all about pytest and how to use its fixtures well. Mypy for use in conjunction with type annotations for static analysis. Packaging: Python-poetry.org Personally, I recommend Deal (design by contract) and/or Hypothesis (property-based testing) libraries, too. Controversial opinion: Stay away from Flask and all of its derivations. That framework is badly designed. Learn from Django instead.
- a_bonobo 3y agoAnother great course is David Beazley's Advanced Python Mastery; he just put it all up on github (PDF of all slides + exercises) https://github.com/dabeaz-course/python-mastery https://github.com/dabeaz-course/python-mastery It's designed as a four-day workshop. Lots of material around 'mature' Python code
- appplication 3y ago> Stay away from Flask and all of its derivations. That framework is badly designed. Learn from Django instead. I couldn’t of agree more. I’m a Python veteran, but hasn’t done web dev before a few years ago. I picked Flask for one of our web apps and now very much regret it. It feels like it fights a lot of the direction modern python is going. It’s also encumbered by an ecosystem of bloated and under-maintained extensions that only make it harder to use. Regarding poetry for packaging, I’m 50/50 on it. The product is interesting, but I find myself almost always reverting to a structure where I have requirements managed by pip-compile. It just feels a lot cleaner. I also had a few unpleasant interactions with the poetry devs with regard of GitHub issues, which makes it easier to obviate on a personal level.
- SushiHippie 3y agoNot a learning source per se, but a good tool to learn "best practices" is using ruff [0] as your linter. I enabled nearly all the rules it has available. And I've learned so much from it. [0]: https://beta.ruff.rs https://beta.ruff.rs
- anyfoo 3y agoUse 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. mypy brings some of the sanity back.
- serjester 3y agoAssuming 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. 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. I’d also recommend checking out Fluent Python.
- a_bored_husky 3y agoFluent Python is fantastic.
- hbcondo714 3y agoYeah, reading the Fluent Python book[1] and / or following along with their support files[2] would a good way to start [1] https://amzn.to/3J48u2J https://amzn.to/3J48u2J [2] https://github.com/fluentpython/example-code-2e https://github.com/fluentpython/example-code-2e
- bitsinthesky 3y agoJust fyi to cheapskates like me, i was looking around for a free pdf, some are hosted on github, others on google docs. Putting the pdf urls into https://www.virustotal.com/gui/home/url https://www.virustotal.com/gui/home/url , all were suspicious or malicious. I think ill search around a bit more, but will probably give up.
- scrlk 3y agoMight be of some interest to you: https://annas-archive.org/ https://annas-archive.org/
- bitsinthesky 3y agoUnfortunately it seems pdfs linked there may also be malicious ( https://www.virustotal.com/gui/url/7e73cc3d685c77d88ad9cf7697245628b90cb94f3e7f54ccb6d4d5da3cfc4225?nocache=1 https://www.virustotal.com/gui/url/7e73cc3d685c77d88ad9cf769... ). I checked on some other pdf links ive downloaded previously and they were safe, so I don’t believe the software just flags everything. But it could be if the coding book includes working examples and exercises, that would probably flag any system looking for code execution.
- deleted 3y ago[deleted]
- ezedv 3y agoImproving 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!
- Aperocky 3y agoAvoid writing java in python. Don't stretch inheritance where where they are not needed - avoid factory classes unless you know for certain that it's called for. Use pythonic stuff like @decorators and enjoy functions as first class objects. Finally, try to avoid using an IDE. This keeps your files and folders structures simple and organized out of necessity. In Java it's almost impossible, but it's very possible in python as it removes so much verbosity.
- anyfoo 3y ago> Finally, try to avoid using an IDE. This keeps your files and folders structures simple and organized out of necessity. A good IDE will keep that structure simple and organized, too. The problem with python is more that its dynamic, duck typing type system hinders some of the great benefits that "modern" (as in, from the past ~2 decades) IDEs bring. Since the IDE can hardly infer any type, and since even the ones it could infer can dynamically change in shape at any time, the IDE cannot provide as helpful suggestions and as powerful navigation as IDEs for other languages can. For the same reason, compile time almost exclusively tells you about crass syntax errors only, which further diminishes an IDE's helpfulness. I'd be curious to know if there are python IDEs who integrate with mypy (or the underlying static typing PEP) to bring some of the lost magic back to python IDEs.
- nick-of-time 3y agoPycharm does, and so does any other jetbrains IDE with their python plugin installed. It's a large part of the reason I bother putting type annotations in my code, to help my IDE help me.
- anyfoo 3y agoGood to know, thanks! I've warmed up to python again when I discovered mypy, having successfully used it for a personal project with rather complicated types (async and thus futures were involved, over more basic types but with complicated shapes). Knowing that there is also adequate IDE support makes it even more of a candidate again...
- gte525u 3y agoType annotations, mypy, lots of documentation. Where python falls down is the flexibility - if you aren't careful it's write only.
- AussieWog93 3y agoWrite Python code and get feedback from a colleague. If you don't have a colleague, GPT-4 is an acceptable substitute.
- brauhaus 3y agoStart reading production level Python. 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! 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. 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. This little habit skyrocketed my Python game
- more_corn 3y agoLearn 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.
- mharig 3y agoSearch the channel of Arjan from arjancodes.com on Youtube. Watch his videos.
- speakspokespok 3y agoGoing 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. [0] - https://www.pythonmorsels.com/ https://www.pythonmorsels.com/
- smithcoin 3y agoAnything like this for golang?
- anoy8888 3y agoThe 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.
- Mawr 3y agoA classic: https://www.youtube.com/watch?v=wf-BqAjZb8M https://www.youtube.com/watch?v=wf-BqAjZb8M (Raymond Hettinger - Beyond PEP 8 -- Best practices for beautiful intelligible code - PyCon 2015)
- extasia 3y agoAll of Hettingers python talks are pretty great.
- deleted 3y ago[deleted]
- extasia 3y agoTry 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. Constantly circle back and refactor. Ruthlessly. 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. Source: 1000s of python hours in engineering and research roles