[HN Gopher] Ask HN: How can I get better at writing production-l...
       ___________________________________________________________________
        
       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!
        
       Author : legobridge
       Score  : 22 points
       Date   : 2023-07-18 18:56 UTC (4 hours ago)
        
       | anyfoo wrote:
       | 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.
        
       | serjester wrote:
       | 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.
        
         | hbcondo714 wrote:
         | 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
        
         | a_bored_husky wrote:
         | Fluent Python is fantastic.
        
       | ezedv wrote:
       | 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!
        
       | AussieWog93 wrote:
       | Write Python code and get feedback from a colleague.
       | 
       | If you don't have a colleague, GPT-4 is an acceptable substitute.
        
       | mydriasis wrote:
       | 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.
        
         | ducharmdev wrote:
         | 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.
        
       | Aperocky wrote:
       | 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.
        
         | tayo42 wrote:
         | Inheriting a python app written by java developers was one of
         | the more frustrating code bases I've worked with
        
         | anyfoo wrote:
         | > 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 wrote:
           | Pycharm 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.
        
       | SushiHippie wrote:
       | Not 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
        
       | [deleted]
        
       | gte525u wrote:
       | Type annotations, mypy, lots of documentation.
       | 
       | Where python falls down is the flexibility - if you aren't
       | careful it's write only.
        
       | nhgiang wrote:
       | 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.
        
       ___________________________________________________________________
       (page generated 2023-07-18 23:01 UTC)