Live data from Hacker News

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

news.ycombinator.com

31–40 of 56 posts

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

#31
post #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.

Another 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

It's designed as a four-day workshop. Lots of material around 'mature' Python code

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

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

[0] - https://www.pythonmorsels.com/

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

#33

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. [0] - https://www.pythonmorsels.com/

Anything like this for golang?

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

#34

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’…

This is great recommendation, you can get very far by reading well written code. These have very well written Python, exposing much of the language expressiveness:

- https://github.com/python-attrs/attrs

- https://github.com/mahmoud/glom

- https://github.com/pytoolz/toolz

- https://github.com/Suor/funcy

- https://github.com/dabeaz/curio

  ps: dabaez has great educational content on writing idiomatic python, definitely worth checking out
An honorable mention of norvig's classic essays, which got me into python while I was in college, over 12y ago: https://norvig.com/spell-correct.html.

Aside from reading code, _writing_ something that you know it exist (e.g glom) and then comparing it to how others have done it is also a great learning experience.

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

#35
post #13

Earlier quoted context omitted.

> 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 i…

Personally, without considering languages, I think IDE subconsciously cause complexity and increase entropy for a project. When things are at the a single key jump it's easy to insert code wherever and eventually build a jumbled mess. I've seen this in too many projects. Going without IDE causes a short term pain but eventually the organization gets there and it speeds up because the code is simply better organized a…

I still have nightmares of a previous job where there was a “god” module with a single file containing dozens of classes, many similarly named, over 7000 lines long.

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

#36
post #13

Earlier quoted context omitted.

> 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 i…

> "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." I've felt this pain a lot. I finally took the time to learn how to make use of the type hinting that python does offer, and now I write those type hints without even thinking about it.…

About using type annotations in Python: Do you get any push back from your team? I do. "Oh, it clutters the code." I don't know how to reply. And, they continue to write complex code without type annotations. It is hard to reason about. It is a never ending battle with people who come from weaker typed language. They struggle to see the value of stricter types.

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

#37

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…

    enjoy functions as first class objects
I roll my eyes every time I hear this trope. What do Python functions have that Java functions do not have? To the average developer, a lambda that maps to the Runnable interface _is_ a first class object.

    try to avoid using an IDE
Ugh. More purism. So now I need memorize more things? No thanks. The IDE can do that for me. Every trivial fact that I need to memorize takes a space in my mind that could be used for more valuable information, including other programming languages.

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

#38

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’…

Second or third reco for Fluent Python. Also, to be a purist for a moment. Explicit is better than implicit. Use this as a razor to decide all ties in your design.

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

#39
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.
Post reply on HN