Ask HN: How can I get better at writing production-level Python?
41–50 of 56 posts
Re: Ask HN: How can I get better at writing production-level Python?
#42Assuming 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’…
Yeah, 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 [2] https://github.com/fluentpython/example-code-2e
Re: Ask HN: How can I get better at writing production-level Python?
#43Avoid 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…
A simple python function also is. Just much simpler. On principal, every turing complete language can do everything that you make it do, including emulating concepts that are more natural in other languages - you can write OOP in C if you want to build the whole scaffold to support it.
I also happen to know quite a number of languages, up to 10 if we talk about everything I've developed something in, but more like 5~ if we talk about ones that I'm familiar with, this includes java and python. imo programming should be language agnostic - don't try to remember the syntax, all of it is available in 10 second via googling, but remember data structures that are common across all languages and specific useful patterns and choose the right one for the job.
Both python and java have their own advantages, and own ways of doing things. But writing python like classic OOP java results in hot garbage. It defeats the purpose of using python, should just use Java if one want to emulate all of that polymorphism and OOP patterns, at least the language is built for it.
Re: Ask HN: How can I get better at writing production-level Python?
#44Assuming 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’…
It is _staggeringly_ dynamic, and basically uses runtime reflection of "static" types to scaffold a really complex ast describing your application.
Re: Ask HN: How can I get better at writing production-level Python?
#45RealPython.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.
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.
Re: Ask HN: How can I get better at writing production-level Python?
#46Earlier quoted context omitted.
Yeah, 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 [2] https://github.com/fluentpython/example-code-2e
Just 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 , all were suspicious or malicious. I think ill search around a bit more, but will probably give up.
Re: Ask HN: How can I get better at writing production-level Python?
#47Earlier quoted context omitted.
Just 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 , all were suspicious or malicious. I think ill search around a bit more, but will probably give up.
Might be of some interest to you: https://annas-archive.org/
But it could be if the coding book includes working examples and exercises, that would probably flag any system looking for code execution.
Re: Ask HN: How can I get better at writing production-level Python?
#48Earlier quoted context omitted.
> "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.
I think you are spot on that people coming from weakly typed languages often don't realize what they're missing. "It clutters the code" is really just "I'm not used to seeing it that way" so if you can get people to try it out (maybe in a well-cordoned area of the code base or in some new dev) they will find out for themselves that it saves time and grief in the end.
Re: Ask HN: How can I get better at writing production-level Python?
#49Earlier quoted context omitted.
> "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.…
I feel a similar way about typescript. It outsource the mental complexity into compile stage instead of directly on runtime.
Re: Ask HN: How can I get better at writing production-level Python?
#50Earlier quoted context omitted.
Might be of some interest to you: https://annas-archive.org/
Unfortunately it seems pdfs linked there may also be malicious ( 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.