Python is the most popular programming language and beloved by many. However I can’t understand why (this is still the case in 2024).

Here are my main gripes with it:

  • It is slow, performance intensive tasks have to be offloaded to other languages, which makes it complicated to analyse. Moreover I wonder how many kwH could have been saved if programms were written in more performant languages. (and there are better alternatives out there)
  • The missing type system makes it easy to make errors, and the missing compiler makes it hard to catch them
  • It has no linear algebra built in, so you always have to convert things to numpy arrays, which is quite annoying
  • Managing virtual environments and pip packages feels overly complicated

I guess much comes down to personal, but I just can’t understand the love for python.

  • BougieBirdie
    link
    64 months ago

    I’m a software developer who primarily uses Python in my day job. I’m a strong believer that every tool, especially in software, has pros and cons baked into it.

    It seems that many of your issues with the language come down to it being an interpreted language, as opposed to a compiled language. Interpreted languages are definitely slower than compiled languages at runtime, but something to consider is also development time. In my experience, a high level scripting language with dynamic typing like Python is easy and fast to write. I spend more time actively writing code than running it, so development speed definitely has appeal to me.

    As to if performance-critical operations need to be written in other languages, I have a little experience with Cython, and I’d say that you’re right - it is more complicated to analyze. However, and I don’t remember where I picked this up, but I’m a strong believer that premature optimization is the root of unreadability in software. Programmers often believe that a portion of their program is going to be slow and then attempt to optimize it without ever having profiled to see if it’s actually that slow.

    Python does have a couple of tools to speed things along for common issues. For instance, list/dictionary comprehension saves on looping time, and using generators can help you control memory usage. Neither truly gives you thee speed of a compiled language, but these are noticeable improvements you can make.

    Regarding the missing type system, there is built in type-hinting you can use which makes your code more closely resemble a statically typed language while still having the flexibility of dynamic typing. The type hinting is optional, which means that in most tutorials or when people are starting out, type hints will probably be absent. However, for enterprise grade software you can take the time to configure a linter to enforce type hints, and I would say it does make your code cleaner and easier to reason around.

    As far as managing virtual environments, I think you’re right on the money there. When I was learning, I found the process confusing, but it becomes more familiar over time. I’ve definitely polluted my global python installation with dependencies that break other projects, and it can be pretty gross trying to sort out the issues.

    Okay, now here’s something that I think is pretty interesting: every one of your main gripes are issues that people also have with Javascript. And that makes sense, there are parallels. They’re both interpreted, dynamically typed languages which people try to use for every problem under the sun. I’d hazard that your issue is that you’re more aligned with compiled, statically typed languages, and there’s nothing wrong with that.

    I think that one of the things that Python and Javascript both have going for them is the ubiquity that they’re used. You can find a pip/npm package for any situation. People start learning one or the other because they hear it’s a good language to get started in, and because you can solve every problem with it, you don’t have to learn another language if you don’t want to. That does mean that people are trying to solve every problem with one tool, and that has problems, but I guess we’ve decided that the pros outweigh the cons.

    Moreover I wonder how many kwH could have been saved if programms were written in more performant languages. (and there are better alternatives out there)

    I just want to say this is an interesting point, and there has been some study into Green Coding and it does definitely seem like we could use our resources more efficiently. However, the insidious side of coding for business is that the demands of business seems to be focused on delivering products quickly at any cost. Even though the science suggests that green coding has positive climate benefits and reduces operating costs, these benefits are intangible to executives and therefore ignored.

    When I was in school, we learned about Moore’s Law which essentially says that computers get faster all the time. In fact, computation speeds tended to increase exponentially, and that has implications on how we write code. Forty years ago, you’d be worried about not using more than eight characters for a variable name because of the implication that has for system memory. Now we don’t worry about those problems, which means that we import more libraries, use more memory, and are less careful about squeezing every last bit of performance out of our machines. It sort of leads to this paradox where we have faster computers, so we write slower programs, which necessitate faster computers.

    Does that mean our code nowadays is a lower quality? I suppose that depends on how you measure quality. For me, readability and maintainability are the most important tenets of writing good code. However, I can’t deny that performance and system usage are important requirements. But optimization often comes at the expense of readability or maintenance (see your concern about using outside languages for performance intensive tasks), so you have to be judicious about how you apply it.

    Finally, I think the ultimate reason that people like using Python is that it’s simple to get started, and there are already libraries / solutions to common problems which are easy to find at a cursory search. Basically, the tool is popular because it’s already popular, so people keep building more tools for it. And sure, there can be better tools that are under development which might one day supersede Python. But for now, the library support in Python is awesome, so there’s little incentive to switch and lose features.