TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: How can I get better at writing production-level Python?

63 pointsby legobridgealmost 2 years ago
I&#x27;m looking for resources that could help me write better, more professional code in Python.<p>I&#x27;ve worked extensively as a software developer in Java before, so I am aware of the many things that go into writing &quot;real&quot; 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&#x27;ve used Python for Leetcode, scripting, isolated ML work, writing little games, etc., and I&#x27;ve read and practiced much of PEP8, so it&#x27;s not that I write bad Python code. But I do feel that I could be doing so much better (since I&#x27;ve done better in Java) and I&#x27;d like to get to that level of proficiency as soon as I can.<p>In that vein, I&#x27;m looking for any resources that have worked for any of you and you think would be suited for me. Thanks!

16 comments

serjesteralmost 2 years ago
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 &#x2F; 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.
评论 #36781263 未加载
评论 #36780036 未加载
评论 #36781753 未加载
评论 #36783304 未加载
评论 #36779795 未加载
Aperockyalmost 2 years ago
Avoid writing java in python.<p>Don&#x27;t stretch inheritance where where they are not needed - avoid factory classes unless you know for certain that it&#x27;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&#x27;s almost impossible, but it&#x27;s very possible in python as it removes so much verbosity.
评论 #36779937 未加载
评论 #36781704 未加载
评论 #36780114 未加载
评论 #36780781 未加载
评论 #36808646 未加载
extasiaalmost 2 years ago
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&#x27;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
nhgiangalmost 2 years ago
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&#x2F;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.
评论 #36781209 未加载
评论 #36786925 未加载
speakspokespokalmost 2 years ago
Going through Trey Hunner&#x27;s Python Morsels [0] exercises is worth your time. Each exercise starts by describing desired outcomes and it&#x27;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:&#x2F;&#x2F;www.pythonmorsels.com&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.pythonmorsels.com&#x2F;</a>
评论 #36781250 未加载
brauhausalmost 2 years ago
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 &quot;See Definition&quot; 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&#x27;ll start writing it yourself.<p>This little habit skyrocketed my Python game
anoy8888almost 2 years ago
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.
anyfooalmost 2 years ago
Use mypy if you can, and then as much as you can. python&#x27;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.
ezedvalmost 2 years ago
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!
mydriasisalmost 2 years ago
Some off-the-cuff tips, at least --<p>TDD is very easy with Python because the unit testing framework is built in -- I&#x27;d suggest writing tests for just about everything you do.<p>Additionally, the typing system is expanding all the time. Make sure you&#x27;re adding type annotations where &#x2F; when you can; even lightweight ones like TypedDict help.
评论 #36779356 未加载
SushiHippiealmost 2 years ago
Not a learning source per se, but a good tool to learn &quot;best practices&quot; is using ruff [0] as your linter.<p>I enabled nearly all the rules it has available. And I&#x27;ve learned so much from it.<p>[0]: <a href="https:&#x2F;&#x2F;beta.ruff.rs" rel="nofollow noreferrer">https:&#x2F;&#x2F;beta.ruff.rs</a>
Mawralmost 2 years ago
A classic: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=wf-BqAjZb8M">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=wf-BqAjZb8M</a> (Raymond Hettinger - Beyond PEP 8 -- Best practices for beautiful intelligible code - PyCon 2015)
评论 #36792740 未加载
AussieWog93almost 2 years ago
Write Python code and get feedback from a colleague.<p>If you don&#x27;t have a colleague, GPT-4 is an acceptable substitute.
more_cornalmost 2 years ago
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.
gte525ualmost 2 years ago
Type annotations, mypy, lots of documentation.<p>Where python falls down is the flexibility - if you aren&#x27;t careful it&#x27;s write only.
mharigalmost 2 years ago
Search the channel of Arjan from arjancodes.com on Youtube. Watch his videos.
评论 #36792755 未加载