Live data from Hacker News

Writing Python like it's Rust

kobzol.github.io

141–150 of 369 posts

Re: Writing Python like it's Rust

#141

Earlier quoted context omitted.

Everything you just said is true for Typescript, as you can set it up as strict or as forgiving as you like, and writing one off scripts for node is just as easy as for Python. But unlike Python, it has a great type system.

With Typescript I have nothing comparable to Django.

It’s mad how there isn’t a Django clone in the JS world. They just stitch together half finished, buggy ORMs, migration tools and web frameworks? After all this time? Something like Django requires focus and concerted effort over a period of many years, so I guess it makes sense.

I get the impression JS devs would rather have a new framework with bugs and cool emojis in the commits than something more stable and less buggy.

Re: Writing Python like it's Rust

#142
I do something similar. You can probably do a similar style in most imperative languages. The key features that make this work in Python are Dataclasses, enums, and type hints. Pydantic if you're looking for a Serde analog. Struct and enum-oriented programming.

Re: Writing Python like it's Rust

#143
This is how I write PHP. The author made a trip to rust to come to this conclusion, what I did was to start using PhpStorm many years ago, that helped me remove many bad habits, like relying on magic and stringly types.

Re: Writing Python like it's Rust

#144

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…

With types (independently of which language) you could encode the knowledge of the sequence you mention. For ex. send_message could take an AuthenticatedClient type. This way, instead of having it all in your head as implicit, it is explicitly described, and in many cases, impossible to use wrong.

Re: Writing Python like it's Rust

#145
post #126

I can’t get over how clunky Python 3 is getting. Does anyone like working with type hinting in Python? All of my code is typed but compared to basically every other type system the process was far more painful than it should have been. Even years later I still keep printouts of the typing documentation next to me so I can save time when I invariably need to look up the multiple ways you can define ‘T’ when it would j…

Personally, I switched to Go.

I've started this transition as well. I love Python, but loathe it's typing and anything to do with asyncio.

Re: Writing Python like it's Rust

#146

Earlier quoted context omitted.

Everything you just said is true for Typescript, as you can set it up as strict or as forgiving as you like, and writing one off scripts for node is just as easy as for Python. But unlike Python, it has a great type system.

Not really. First, the JS ecosystem is very web oriented, so if you want to dabble out of there, you often gonna fall short. Secondly, the JS packaging has very poor support for compiled extensions, which mean everything that needs a perf boosts is unlikely to get good quality treatments. Finally, the community makes it a constant moving target. After 20 years of writing both JS and Python, I can still install old dj…

Python has really poor support for compiled extensions. I know this sounds weird to say, given that they are used everywhere, but this is the number one pain point in Python. It’s really awkward to say, develop on Mac and deploy on Linux.

Re: Writing Python like it's Rust

#147
post #77

Earlier quoted context omitted.

Typescript is of course nice and is a great type system but can’t totally paper over the absolutely impoverished base language that is JS. When you use Python and things like equality do what almost anyone wants instead of checking object identity, it’s real annoying to futz around in TS

[1, 3, 12].sort() == [1, 12, 3] 5 in [5] == false

Even setting the sort order aside, just the fact that it both returns the sorted array and also sorts it in-place is not great. I prefer Python's distinction between sorted (returning a sorted copy) and list.sort (sorting in place and returning nothing).

Re: Writing Python like it's Rust

#148

Earlier quoted context omitted.

A good IDE like PyCharm makes it pretty simple, will offer to write all the import statements for you when you reference something that's new to the file you're working on. I'm taking a pretty decent course on Udemy right now, but the instructor said they didn't see the value in PyCharm and they're writing all this extra boilerplate by hand in VSCode. I started using copilot recently and that's accelerated the proces…

These are not complicated tasks that should require a plugin or an AI assistant to make bearable. Just add the syntax to the language.

> These are not complicated tasks ... Just add the syntax to the language.

Hahah, I've never designed a language, but I've been around long enough to know these 2 phrases are incredibly at odds.

Re: Writing Python like it's Rust

#149
post #100

Lots of comments here are stating that typing is half baked in Python, and that if you gotta use types, you should use another language. But that's missing the point that Python is still not meant to be the best at anything, but good at most things. And in this case, it's exactly what you get: optional typing, with decent safety if you need it. You can quick script or design seriously, you can explore in a shell or c…

On the one side, yes Python is incredibly versatile. On the other side, I have worked on many Python projects, some of them fairly high profile, and I have seen exactly two kinds of Python codebases: 1. a few were written by extreme professionals, plugging at every single hole, with ~100% coverage, plus considerable maintenance because every dependency upgrade tends to break something; 2. many that feel cobbled toget…

I would like to see a codebase of the first kind. All larger python projects I have seen are of kind 2...

Re: Writing Python like it's Rust

#150

Lots of comments here are stating that typing is half baked in Python, and that if you gotta use types, you should use another language. But that's missing the point that Python is still not meant to be the best at anything, but good at most things. And in this case, it's exactly what you get: optional typing, with decent safety if you need it. You can quick script or design seriously, you can explore in a shell or c…

I agree.

In some cases I hate when I have to use Python for a task that really deserves full typings because Python’s type hints are so half baked. But all other times, I like just sprinkling them in where relevant. Python is great at being okay at everything. That’s a real power.

Post reply on HN