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!
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.
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.
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. It makes the development experience so much better for me and across the team, since we've all bought in. It doesn't do everything. It is kind of lipstick on a pig. But it's worth doing.
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.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.
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.
- 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.
It is _staggeringly_ dynamic, and basically uses runtime reflection of "static" types to scaffold a really complex ast describing your application.
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.
It's designed as a four-day workshop. Lots of material around 'mature' Python code
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.
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.
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.
Constantly circle back and refactor. Ruthlessly.
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'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.
Source: 1000s of python hours in engineering and research roles
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!
Even the standard library is worth seeing. Next time you import pathlib.Path, right click it, select "See Definition" and go find out how the sausage is made.
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'll start writing it yourself.
This little habit skyrocketed my Python game
mypy brings some of the sanity back.
I enabled nearly all the rules it has available. And I've learned so much from it.
[0]: https://beta.ruff.rs
If you don't have a colleague, GPT-4 is an acceptable substitute.
Where python falls down is the flexibility - if you aren't careful it's write only.