Loading…

Why do I hate Python?

If you know me well, you know very well that I can’t stand Python as a programming language. If you don’t know me well, then make sure it’s no secret anymore that, as both a student and a skilled programmer, I can’t stand Python. But why?

There are many reasons why I hate Python ranging from language concepts, to syntax, to “features”, and more. I can’t stand its weird, indentation-driven syntax, I have a major disdain for immutable strings, and do NOT get me started about the differences between Python 2 and Python 3. Let’s delve into this a little.

Python’s Stupid Syntax

Forgive me for my swearing but, I have drum and bass music going as I write this (typical me!) and need to get this out in a way that’s extremely meaningful. Who the fuck in their right mind decided it’s a good idea to formally require proper indentation in their language’s syntax? Take a look at these two code snippets – one is C++, and one is Python.

#include <iostream>
#include <string>

int main()
{
    string word;

    std::cout << "Please enter a word: ";
    std::cin >> word;

  std::cout << "You entered " << word << "." << std::endl;

    return 0;
}
def main():
    textEntered = input("Please enter some text: ")
    print("You entered the text: ")
  print(textEntered)

main()

In both examples, I purposely used inconsistent indentation levels in my code – one line is indented with two spaces and the others with 4. Most languages (including C++) are okay with this at compile-time as they’ll skip the whitespace completely during lexing. But not Python! If I run that Python code, I get this output:

Traceback (most recent call last):
  File "<string>", line 4
    print(textEntered)
                     ^
IndentationError: unindent does not match any outer indentation level

See that? IndentationError: unindent does not match any outer indentation level. The interpreter is complaining about that one line that I screwed up my indentation on, and refusing to run my code.

This is because Python uses indentation in much the same way that C++ uses curly braces – to define scope. The way it works is that a block of code begins with some sort of statement (function definition, if, while, for, etc.) then a colon, and after that colon, all code that’s a part of that block has to be indented by one level. The code block ends at the first line where the indentation is at the previous level.

The interpreter won’t know what block of code a line is a part of if it doesn’t match any surrounding indentation levels – like that one print statement. The proper syntax of that code would be:

def main(): # Start of function block
    textEntered = input("Please enter some text: ")
    print("You entered the text: ")
    print(textEntered)
# End of function block

main()

And that’ll run. But I haven’t explained why this is stupid and over-complicated. I can explain it in a single paragraph though.

While Python uses indentation levels to determine what block of code a statement is a part of, C++ as well as C#, JavaScript, C, Java, Lua, PHP, and several other languages either use curly braces or some other delimiters to determine both the start and end of a codeblock. Therefore, Python’s indentation-based syntax is the dumbest non-esoteric syntax I can think of.

Seriously! If you’re a programmer, think of how many times you’ve accidentally typoed and messed up one line of code’s indentation? Or maybe you’re on a team and two people with different indentation settings are working on the same file? Maybe one person uses tabs and another uses spaces? Maybe one uses 4 spaces and another uses 2 or 8?

While I don’t like inconsistent indentation, this should be a conventional issue, and oot a syntactic error. If you’re on a deadline, do you really want to spend time fixing indentation errors in your codebase or do you want to focus on making sure everything works correctly? I choose the latter, and that’s why I think Python’s syntax is stupid.

Immutable strings

For those who don’t know, immutable means “you can’t change that obect’s data.” In Python, this is the case with strings of text. Take these two pieces of code, one is Python and one is C#:

def swapChars(text):
    lastChar = text[-1]
    text[-1] = text[0]
    text[0] = lastChar
    return text

userText = input("Enter some text: ")
print("We swapped the text to say: ")
print(swapChars(userText))
using System;

namespace MyProgram
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter some text: ");
            string text = Console.ReadLine();

            char last = text[text.Length - 1];
            text[text.Length - 1] = text[0];
            text[0] = last;

            Console.WriteLine(text);
        }
    }
}

The C# code will run just fine. I write “Hello World”, it says “dello worlH.” Python, however, will let me input the text then yell at me:

Enter some text: 
Hello World

We swapped the text to say: 
Traceback (most recent call last):
  File "<string>", line 9, in <module>
  File "<string>", line 3, in swapChars
TypeError: 'str' object does not support item assignment

TypeError: ‘str’ object does not support item assignment.

This is because, in Python, strings are immutable. You can’t change the individual characters in a string. I hate this. I know the reason why, but I hate this.

Python’s equivalent of a C# Dictionary requires its keys to be immutable, and to allow strings to be used as a keytype, strings must be immutable. That’s the reason strings are immutable in Python.

The reason I hate this is because there are actual algorithms out there where I may want to mutate a string like that! There’s the swap algorithm from above, maybe I want to insert a character in the middle of a string, maybe I want to replace a value at a specific index with another…

So mant extremely trivial tasks in C# that are far more difficult than they need to be in Python and… ugh. I hate this.

Python 2 vs. Python 3…

This goes off of one example only but it’s still annoying. Python 2 is very different from Python 3 syntactically. A few days ago, I was using Node.js to develop Project: Cassian. I was installing chartjs-node through npm so I could render charts in the user interface.

The package was trying to run some Python code, as some sort of preinstall script or something. Actually, I think it was just trying to print a formatted string. At first the package install script couldn’t locate a Python binary…so I had to add my Python insstall (for school) to my PATH.

Add it to the PATH, reboot, try again.. and it still fails. Look at the error, and it’s a Python syntax error. Why? Because the code was in Python 2 and 2 and I am learning Python 3.7 at school and thus have that installed at home. The syntax error was highlighting a print statement, which is different in Python 2 than in Python 3.7. So, syntax error.

I eventually ended up installing chart.js through npm instead, which didn’t throw any Python errors like that… but this wouldn’t have happened in the first place if the syntax of the language didn’t radically change its syntax from version 2 to version 3 in a way that breaks something as simple as a damn PRINT STATEMENT. Argh. I hate Python.

In conclusion…

Maybe I’m wrong. Maybe it’s just my lack of experience with Python, but with these experiences, it just makes me want to use different languages instead of learning this one. Argh. Rant over.

One thought on “Why do I hate Python?

Leave a Reply