Live data from Hacker News

Writing Python like it's Rust

kobzol.github.io

71–80 of 369 posts

Re: Writing Python like it's Rust

#71

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 m…

> I can do almost all the dynamics things that I can do in Python with C#, but in a type safe way. If you like flexibility with a good type system, give Typescript a try, its miles ahead of mypy and its designed by same person who’s behind C#.

I don't think this is a completely accurate comparison, as TypeScript is transpiled to JavaScript and this provides restrictions to the expressibility and usability of types at runtime, which I've sometimes hit my head against. C# has much more type information available at run time, as does Python.

Re: Writing Python like it's Rust

#72

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…

Would that all our codebases be that straightforward...

I worked in a team with low skill. One of our flagship apps had a lot of inherent complexity, and was coded by an over-promoted fool. He wrote spaghetti that was vile even at launch.

To my complete lack of surprise, after launch, the users wanted an enormous list of changes and new features. When you try to add features to code that's already spaghetti, the complexity compounds.

There's only one way to manage that, and it's to refactor. But to refactor you need good tests, and your team needs to accept the use of resources for refactoring.

This dungheap had objects with fucky interlocking responsibilities, e.g., scheduling was partly done by the ORM classes and partly by the class for customer output.

This app was also extremely time-based. A CRUD app doesn't really require you to think about how state changes over time; but in the dungheap, you couldn't "see" the state from the code, you also had to understand when in the sequence of operations a method was called. This is one of the hardest types of complexity to deal with.

The app was completely untyped. Some functions take enums and others take strings. There were state enums and also free text strings for state. You might see both "FAILED" and "FAILURE" for the same state concept. A huge amount of data was passed as nested dicts, and without the benefit of consistent kwarg names, so you would not know whether the variable you're looking at has an "error" or an "err_msg" key, or an "output" key containing a dict with an "error" key. To find out, you had to run the app for several minutes with a bunch of print statements, and the answer you got might vary depending on any of the 20 input flags.

I generated a call graph to try to grok the sequence statically but the graph was spaghetti and the sequence contained loops. A few code paths called methods once with one datatype and then again with another type.

Type hints massively reduced the cognitive burden. My violent impulses towards our dickhead "architect" reduced from daily occurrences to weekly.

Fwiw, TypedDict turned out to be a great fit for the use case.

Re: Writing Python like it's Rust

#73

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 m…

A typical pain point where type hinting fails in Python is with list of some types, it can't be enforced by the language without more checks.

For example, you can't do:

if isinstance(x, list[int])

though it would be super useful

Re: Writing Python like it's Rust

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

Yes, you can write Python code that runs with missing or wrong type hints. Not ideal, but you can add a static type checker (mypy) as a step in your CI pipeline and reject commits that fail this step. Not much discipline required.

Re: Writing Python like it's Rust

#76
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 commit a public API into a file, you can start with untyped code and add some later.

That makes it cumbersome sometimes, yes. mypy is slow, and the typing system + match / case have many rough edges.

But it also keeps Python incredibly versatile.

It's amazing what you can do with this (not so) little language, and the whole ecosystem is always getting better and better.

Every time I try another language, I keep getting back into Python, because the fields I need tools for are vast, and it's the only one that I'm pretty sure will handle a problem decently in all it's various forms, especially the one I didn't consider when I started.

There is immense value in this, because it keeps opportunities on the table no matter what you do. I often find my self during a project adding something I could only because Python let me do so easily.

Python is quirky, yet it still damn practical.

Re: Writing Python like it's Rust

#77

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 m…

> I can do almost all the dynamics things that I can do in Python with C#, but in a type safe way. If you like flexibility with a good type system, give Typescript a try, its miles ahead of mypy and its designed by same person who’s behind C#.

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

Re: Writing Python like it's Rust

#79
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 follow that convention. I've written many functions that accept a list of values and, typically as intermediate processing, build a dict from them. Many rest APIs I've coded for require me to fetch from two endpoints to build my output, and this pattern saves iterating over lists.
Post reply on HN