Live data from Hacker News

Writing Python like it's Rust

kobzol.github.io

1–10 of 369 posts

Re: Writing Python like it's Rust

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

Re: Writing Python like it's Rust

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

Re: Writing Python like it's Rust

#4
So cool. I love rust and my day job is in Python.

But I have a question. I'm a junior dev(gimme some leeway here).

I don't really understand how important these design patterns are because in the programs I write, I usually write the classes and call them in runtime myself. I think usually we write the servers and client ourselves.

Let's take the different client types example. You are making an assumption that users can call close on a closed client. Is it so hard to just follow the sequence

` client = Client()

client.connect()

client.authenticate()

client.send_message()

client.close() `

Aren't they overengineering? Perhaps I have not worked in a large code bases to understand the problems TypeState Pattern or more generally these design patterns solves.

I understand that these patterns are elegant and make the future modifications or enhancements easier but I have never seen the tangible value enough in real life

Re: Writing Python like it's Rust

#5
Type hints are a great feature in Python to enhance readability and to catch programming errors when refactoring or learning a new library.

A lot of the features presented here are helpful for programming in the large, i.e., once the program exceeds mental capacity.

As a general goal I think that programmers should reduce complexity even of simple stretches of code so that, adding all its context, a larger chunk of code fits into their brain at once and errors become more visible. Part of that is unloading tasks onto the tools.

Unfortunately, its still common practice to create write-only code, often justified by an "it works" (usually not true).

Re: Writing Python like it's Rust

#6

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?

Re: Writing Python like it's Rust

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

Re: Writing Python like it's Rust

#8

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.

In my experience, due to not needing a plugin (and not having the completely not checked BaseSettings), typedload is better in respect to runtime type safety and using mypy.

I designed it keeping mypy in mind from the beginning.

Re: Writing Python like it's Rust

#9
I've seen a bunch of code like this, usually written by juniors once they get a bit of experience and discover the concept of types.

A surefire way to make your Python code terrible. The whole point of Python is the duck typing. If you're not going to use that then you might as well use a real programming language.

Re: Writing Python like it's Rust

#10

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?

He means he didn't benchmark it against a compiled language :D :D :D
Post reply on HN