Live data from Hacker News

Writing Python like it's Rust

kobzol.github.io

41–50 of 369 posts

Re: Writing Python like it's Rust

#41

Cool post, I've also been using more and more type hints and it really makes writing python more enjoyable. Although unfortunately it's still quite common for libraries to not have type stubs. On the section of construction functions, I've always used/seen `@classmethod` instead of `@staticmethod`. I just had a quick look at some big python libraries (pandas, transformers), and they do use class methods. Is there any…

FWIW personally I use @staticmethod when I have small-ish "utility" methods that are only ever used in-class. Basically just a convenient way of organizing code and making the intended context clear.

Re: Writing Python like it's Rust

#42
post #7
post #3

> Is records a list, a dict or a database connection? Records is a list of record items. It’s a plural. If it was a dict, it would be recordByID. If it was a database connection it would be called connection. Strict typing is good, and you should definitely use it, but you should still have good names.

>If it was a dict, it would be recordByID. Never have I ever encountered any codebase where dictionaries were so named.

I usually add a “_dict” to the variable name.

Re: Writing Python like it's Rust

#43
I went through a similar journey, without the Rust part. Started using type hints, data classes, pydantic to get the benefit of static typing after dealing with the pain of refactoring dynamically typed projects.

It was better, but it _feels_ like lipstick on a pig. I love Python, but types are not what it's best at. It's missing features that makes typing easier.

I've realized if was going to write typed Python, I might as well switch to one that has better support for that and switched to .NET.

I can do almost all the dynamics things that I can do in Python with C#, but in a type safe way. Expressions and extension methods, reflection with `nameOf` opens up a lot of possibilities.

One thing that kept me using Python was SqlAlchemy, an ORM, a damn good one. Entity Framework Core, after V6 is a better one, especially with LINQ.

If you have the opportunity, give it a try. You might be surprised.

Re: Writing Python like it's Rust

#44
post #28
post #3

> Is records a list, a dict or a database connection? Records is a list of record items. It’s a plural. If it was a dict, it would be recordByID. If it was a database connection it would be called connection. Strict typing is good, and you should definitely use it, but you should still have good names.

> Strict typing is good, and you should definitely use it, but you should still have good names. Linters and type checkers will understand type hints. They won’t understand your naming conventions.

I know. That’s why I wrote that strict typing is important. But naming variables properly helps your colleagues now and you in the future when you need to come back to this code.

Re: Writing Python like it's Rust

#45

Tooling has come a long way in the past few years to help support better Python. Nowadays I can just throw on Pydantic V2 (still in Alpha, but stable to code against for basic use cases), ruff and mypy. Turn strict mode on in mypy and install the relevant vscode extensions and you get an experience that rivals a compiled language in both speed and usability.

What do you do when your dependencies don't have types? Also, with speed do you mean compilation or runtime?

I mean compilation time, obviously ;)

Re: Writing Python like it's Rust

#46
post #33

Earlier quoted context omitted.

Languages that you would build serious production software in, rather than some script. C, C++ are the main ones.

Lots of production software is written in Python, for better or for worse. In fact, production software gets written in every language eventually, no matter the intent of its designers. Maybe you only write your production software in C and C++, in which case good for you.

I do write Python code used in production, but does that doesn't make it serious production software.

It takes a lot of effort to make a Python program not break on some inputs, so it's not really fire and forget like it is with C++. It's possible but it requires many more iterations.

Re: Writing Python like it's Rust

#47

I started my programming journey with C# which is a statically typed language, and when I later was introduced to Python when I was in college, I really liked how convenient it is but the lack of typing made me really uncomfortable.

There's typing in Python. Strong typing even. It's just that it's also dynamic.

Re: Writing Python like it's Rust

#48
post #32

Earlier quoted context omitted.

Languages that you would build serious production software in, rather than some script. C, C++ are the main ones.

So you deny that the industry used Python to build serious production softwares? Understood, no need to argue then, I don't think any amount of proof would change your mind...

I also do not consider websites or smartphone apps to be serious software, despite the huge amount of work that happens to be done there.

You know the rule, 99% of everything is crud.

Re: Writing Python like it's Rust

#49
See how much discipline is needed to "write Python as Rust"? This is the real problem: you can have great code in dynamic/liberal/forgiving languages, but it will be due to programmer's discipline.

Using a tool like Rust (or other strict/strongly-typed languages) forces some quality constraints on all code that compiles. This is, to me, a great benefit of these languages.

Post reply on HN