Live data from Hacker News

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

news.ycombinator.com

1–10 of 56 posts

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

#1
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!

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

#2
Some 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.

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

#3
RealPython.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.

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

#5
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.

mypy brings some of the sanity back.

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

#6
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.

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.

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

#7

Some 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.

When 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.

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

#9
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!

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

#10
Avoid 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.

Post reply on HN