Live data from Hacker News

Writing Python like it's Rust

kobzol.github.io

321–330 of 369 posts

Re: Writing Python like it's Rust

#322

One of the less talked about effects of type hinting in Python is that editor integration gets a lot better. Better autocomplete is a great help.

Yep. This is actually one of the most useful features for me, autocompletion and Go to definition. I don't actually even use an actual type checker in most projects, unless they are more production-critical.

Re: Writing Python like it's Rust

#323

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…

I don't think that it's a good idea in general to inherit the construction functions. But if you wanted to do that, classmethod is the way to go, of course.

Re: Writing Python like it's Rust

#324

> I have no idea what is going on from the signature itself. Is records a list, a dict or a database connection? Is check a boolean, or a function? What does this function return? What happens if it fails, does it raise an exception, or return None? [...] This type of objection is only raised by people who have never seriously used Python before. It sounds convincing that this is a serious flaw in Python, but in prac…

I have used Python for 10+ years, and I'm able to forget what are the input arguments/return type of a function after 30 minutes of working on some other part of the code :)

Re: Writing Python like it's Rust

#325
post #124

Earlier quoted context omitted.

Most good databases actually store data per column. So you'd have a list of batches, each batch being a dict of string to list of T.

The article mentions connection, so it’s the connection the DB rather than the data. Ie the thing you get after you open the socket.

Hence the batching.

Re: Writing Python like it's Rust

#326
post #231

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 realize Instagram, Pinterest, and a whole host of other unicorn software companies started out with python (often using django) right? And many, many current startups continue to do so.

They're websites, come on.

Re: Writing Python like it's Rust

#327
post #35

What is the smart money doing for type checking in Python? I've used mypy which seems to work well but is incredibly slow (3-4s to update linting after I change code). I've tried pylance type checking in VS Code, which seems to work well + fast but is less clear and comprehensive than mypy. I've also seen projects like pytype [1] and pyre [2] used by Google/Meta, but people say those tools don't really make sense to…

I have extensive experience of MyPy and Pyright, even going so far as to try to fix a but in MyPy (I couldn't do it; the code is too much of a legacy undocumented mess). Pyright is much much better. If you're starting a new project or work with people who understand the value of type hints and want to actually fix them (ha yeah right) it's a no brainer. It's also the default in VSCode which is nice. Oh and they guy t…

I also have extensive experience with mypy and pyright, also even going as far as trying to fix a mypy bug (also unsuccessfully). For pyright, the dev was so responsive that I only had to report bugs for them to be fixed (sometimes in a couple of hours).

It's been a year or two since I've touched Python, but back then, Pyright, was way faster, was more feature-complete, had a much more responsive dev team (of 1). It was better literally along every metric: except that it didn't support custom extensions to the type system like mypy did. But that wasn't a huge issue since there were very few extensions, and even the one that was developed by a mypy core dev for sql-alchemy was hopelessly out-of-date, or impossible to get working. So I didn't miss it much.

All this to say: pyright was much better.

Re: Writing Python like it's Rust

#328
post #314
post #303

I really like type hints in Python. It allowed me to catch several errors before running code. But it isn't always the most straight forward process. However, my most frequently occurring thought is: I'm doing all the typing, I wish I could get some performance benefits out of it.

Good point! Guido mentioned that maybe in the future, the new "mini-JIT" in CPython might be able to leverage the type hints. I also want to have this as a thesis topic for my students, to try it out.

That would be interesting! You might already be aware. But there's mypyc[0], which is an AOT compiler for Python code with type hints (that, IIRC, mypy uses to compile itself into a native extension).

Wanted to give you a head-start on the lit-review for your students I guess :)

[0] https://github.com/mypyc/mypyc

Re: Writing Python like it's Rust

#329

Earlier quoted context omitted.

Please don't. This is about people trying to use the wrong typing system "static typing" in a language that already has a better typing system "dynamic typing". This is about bad programmers who have come from Java and want all other programming languages to look like Java.

Not better. Different. Languages make different choices to suit different domains, and with perfectly good reasons. Don't complain, embrace the richness and diversity of programming languages. And choose the right one for each task.

It's better in the context of a programming language designed to use it.

Re: Writing Python like it's Rust

#330
post #307

Earlier quoted context omitted.

This may shock you but there is effort involved in learning how to use Python effectively.

What has this to do with my comment?

"Others take 30 minutes"

If you knew what you were doing with Python, you wouldn't have this problem. Obvious solutions include unit tests and pickle.

Post reply on HN