Does Python really sucks ?

Naren Yellavula
Dev bits
Published in
7 min readNov 6, 2023

--

generated by vidura.ai

Hi dev community. I want to come up with a new topic to address few questions generally raised about the Python programming language in a buoyant manner. I recently came across few negative opinions about Python programming language which are similar to: “Why Python is the worst introductory programming language for kids and adults ?.” etc. It is a frequent tendency in developers coming from other programming languages.

The top 5 Python complaints I hear are:

  1. Indenting code is horrible
  2. No strong typing
  3. It’s slow
  4. Not good for large code bases
  5. Import system is unpredictable

I think people have somewhat misunderstood Python language that had evolved over past thirty two years(1991-present). Python is definitely not a new language, and like legacy software, it is shaped into what it is today. In this article, I am going to clarify some of the common misconceptions for new programmers and explain why those surprising details exist in the Python language.

Introduction

Leetcoders(competitive programmers) suggest to use Python, do so because it allows them to right code faster instead of Java or Go. Similarly, researchers and scientists have picked Python as their main language to create modern machine learning packages(Ex: sci-kit, pandas) due to it’s simplicity. If you go over Python’s philosophy “Zen of Python”, you can clearly see one of the principles:

Simple is better than complex

But, simplicity can sometimes be easily misinterpreted as “Incompetent” or “Toy-like” nature. As part of it’s simplicity, Python lacks the object-oriented nuances of Java nor niche foothold like Erlang or C++. That doesn’t mean Python can’t express powerful ideas. In contrast to that idea, Python allows one to communicate both to computer as well as programmer. Result ? most of the ongoing research papers in the field of Artificial Intelligence are implementing their algorithms in Python.

Python’s language design is strongly influenced by simple choices. It means when there is a decision to be made, Python prefers to go with simple naming, light-weight packages etc.

Now let’s see why few complaints about Python and how to clear those myths by presenting few grounding reasons.

Indenting Python code is horrible

If you are coming from other programming languages like C, C++, C#, or Java to Python first thing that hits you hard is the indentation of code and lack of flower brackets {}. Why Python code needs four-spaces or a TAB, to put a bunch of statements into a logical block ? Why can’t they follow others by having curly brackets {} to define a function or a logical condition ?

This is the first thing people notice in Python. They may also get interpreter errors when they do mistakes in indentation and try to run the Python program. Let’s visualize this phenomena by comparing a simple C program to find the maximum element in an integer array with it’s Python counterpart.

As you see here, we defined a function called `find_max` that finds maximum integer from an array of integers. Lines 3–11 defines the function. Lines 6–8 defines an if condition. Because this is C, you have curly braces {}, to identify the scope of a given block. It sounds logical in the first place, but you can also notice that lines 4–10 are indented for readability despite having the natural boundaries for C compiler. Readability ? there you go. C code requires hints for compilers as well as humans and therefore it uses both curly braces and space-based indentation. Now, let’s see Python variant.

This is exact re-write of above C program, and as you see Lines 1–6 defines a function. Lines 4–5 defines the `if` logical condition. The indentation (4-spaces) here tells interpreter that every instruction that is right-indented-to a top-level construct belongs to the construct. At the same time, it is also giving readability to programmers to clearly see the boundaries of code.

Two birds with one single shot. Isn’t it ? Python here made a logical decision to use new-line(\n) as a natural boundary for functions and logical statements. One can argue it’s tough to see indented blocks which are 100-line long. I agree. It gets messy when you have a function with 100 lines, but in that case, you should think of breaking your code into smaller functions for readability.

No strong typing

Many stress that Python lacks strong types like in C, Java or Go. Yes, Python lacks types but does any other dynamic programming language like JavaScript, Ruby, or Lua. That is the nature of a dynamic programming language. Python’s lack of primitive types is internal to it’s language design. Everything is an object in Python, including functions. To verify this argument, you can run this statement in a Python shell with any datatype.

isinstance(1, object) # Returns True

You can ask “Why interpreted languages don’t have strong types ?”. Answer is because they are interpreted. An interpreted language reads and executes programs in one single shot. In contrast, compiled languages can scan the program to see any possible type mismatches and report them before executing the actual compiled binary. The biggest benefit of coding in a dynamic language is it’s interactive feedback or REPL(Read–eval–print loop). Python chose it to be that way because dynamic programs gives feedback faster to users at the cost of static type checks.

Does this dynamic nature of Python hurts projects ? It depends. If one wish to have a code base with no unit tests and want to rely mostly on a compiler to catch software bugs, then Python is a right choice for them. While using Python, writing unit tests, performing exception handling, can improve code quality and maintainability of a large code base. We will talk about it more in later sections.

Python is slow

Slow is a relative term. An object A can be faster than another object B while being slower than object C. While making comparisons, we should give higher priority to “What is acceptable performance for the given use case ?” vs “Is Python slower than Java ?”. For example, in an Input/Output (I/O) bound task, a program waits for an answer from a connected network instead of computing an immediate answer. And not all tasks are CPU-bound. Most of the web serves are rarely CPU bound. Python is really good at acting as a glue system in between multiple network systems, and leverage it’s asynchronous I/O capabilities.

If one needs a CPU bound operation in critical path of their Python application, they can offload it to some other server using RPC or HTTP API which is implemented in performant, low-level programming languages.

So, saying Python is slow is like confirming “Earth is Large”, but in what context ? Earth is large when it is compared to humans or Himalayas. But it is small in relation to other celestial objects like Sun, Jupyter, or Saturn. Python in the context of building control planes is definitely acceptable, but may not be a right candidate for implementing a CPU-bound, high-performance database. So saying “Python is slow” doesn’t do justice in most of the use cases.

Python is not good for larger code bases

This is another key argument I hear a lot about Python from developers who are migrating from other programming languages. The further reasons I get are:

  1. No types
  2. No good IDE auto-suggestion support
  3. Hard to debug logical errors

We already discussed about “No types”. Having good unit tests can definitely help maintaining the code, and is not a strong reason to avoiding a language for large code bases.

Python IDE auto-completion used to be behind of statically-typed languages like C# or Java a decade ago. But not anymore. With modern IDEs like PyCharm and VSCode, and with dedicated plugins, we can’t feel any difference.

Debugging logical errors in a legacy enterprise project is a nightmare due to multiple reasons in code:

  1. Poor naming
  2. Functions violating Single Responsibility Principle (SRP)
  3. Shared global state
  4. No test cases
  5. Stuffy modules/packages

These issues can degrade debugging experience of any project written in any programming language leave Python. If you don’t agree with me try to debug a large-scale badly written Go project. You will feel hate your life.

Finally, given that industry is moving towards smaller units of computation like micro-services or server-less functions, larger code bases will gradually transform into multiple code bases in a decade or so. Perhaps those code bases can happily be in Python.

Appropriate testing discipline can help build large complex applications in Python as well as having interface specifications would — Python.org

Python’s import system is unpredictable

Beginners in Python also complain about how frustrating it is to debug module import errors in Python. But most don’t read the documentation about python import system and modules before working on projects.

There is a big difference in the packaging system of Python and languages like Java. Java packages are namespaces for classes, whereas Python packages are namespaces for modules. They are not same.

Conclusion

Python like any other programming language is no silver bullet. A newcomer can appreciate Python more by knowing it’s history and nuances not just the syntax. I like Python because it has inspired many domains like algorithm development, machine learning, and scientific computing due to it’s shallow learning curve compared to statically typed, nuance-rich, object-oriented programming languages like Java. So to answer my initial question:

Does Python really sucks ? No. Not at all, if you care to understand it well.

Further reading:

--

--

Naren Yellavula
Dev bits

Experienced software engineer. "To understand the universe, know yourself"