Live data from Hacker News

Writing Python like it's Rust

kobzol.github.io

21–30 of 369 posts

Re: Writing Python like it's Rust

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

Hmm, could you explain what makes it terrible?

Re: Writing Python like it's Rust

#22
The thing with type hints in Python is that it's not enforced unless you make and maintain checking it as part of your workflow. And it comes with a bunch of things, at least: make sure to configure mypy, make sure you run mypy after push (experienced pythonists will forget to run it before), make sure everyone's IDE has it integrated, make sure to be super clear on which version of mypy you run (so errors people see in their IDE are the exact same as on CI). And don't forget to update all this when new version comes out.

Re: Writing Python like it's Rust

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

The whole point of Python is to interoperate with existing Python code, which is unfortunately already everywhere and won't go away.

Re: Writing Python like it's Rust

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

Most people don't know a real programming language. (And using types in Python/PHP is a good way to start.)

Re: Writing Python like it's Rust

#25

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…

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

One of the biggest learning moments in my work has been collaborating my past self. You need to have stepped away from some piece of code for a while to get this. If the code is not well organized you will think an alien wrote it.

It's only over time you realize that "it works because I know how to use it" is actually a problem. Over time you also figure out how to write the code in a way where you aren't surprised at your own decisions.

Re: Writing Python like it's Rust

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

> I've seen a bunch of code like this, usually written by juniors

Yes and I'm going to be honest, my first worry when type hints came around is that the language would be inundated by the java drones where they would enforce this 100% of the time.

Instead it seems people are mostly using it where it makes sense (APIs, etc)

Type hints are a great asset. You don't have to use it everywhere. And it should remain that way

Re: Writing Python like it's Rust

#27

> Dataclasses instead of tuples or dictionaries The point is good in general, but it's still perfectly possible to use tuples and get all the field name and typing benefits: just use typed named tuples [1]. Unfortunately this is buried in the typing module. I couldn't even find it in the table if contents, and I knew what I was specifically looking for. https://docs.python.org/3/library/typing.html#typing.NamedTu...

[deleted]

Re: Writing Python like it's Rust

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

Re: Writing Python like it's Rust

#29
post #12
post #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.

Can you enumerate the languages that you deem "real" and those you don't? I have a very hard time understanding what meaning you give to that word.

Languages that you would build serious production software in, rather than some script.

C, C++ are the main ones.

Re: Writing Python like it's Rust

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

Type hints communicate intents. They can be used for many things.

Django-Ninja use them to do dependency injection and generate schemas for your API (using pydantic). autodoc use them to generate the documentation of your function. etc...

It does not replace duck typing, proof here: https://peps.python.org/pep-0544/

Post reply on HN