Python 2.7 is ugly

27 Dec 2016

Since I've joined Google, I have been writing Python 2.7 intensely. I've found that the language is ugly. Here are some specific points:

Calling a super class' method requires us to repeat the class name.

Here's how Python 2.7 calls a super class' method:

class YourClass(BaseClass): def __init__(self): super(YourClass, self).__init__()

YourClass is repeated. This is prone to error. Someday, someone will make a mistake of writing super(BaseClass, self).__init__().

Python interpreter should have been smart enough to handle super.init().

An instance method's first argument requires self as its first argument.

Python interpreter should have been smart enough to figure this out without specifying self as the first argument. This leads to confusion, in general, and a strange error message:

>>> class T(object): ... def test(self, a): ... print a >>> t = T() >>> t.test('a', 'b') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: test() takes exactly 2 arguments (3 given)

test(..) is called with 2 arguments, but the error says 3 is given.

datetime.date cannot be mocked.

For some reason, a dynamic language like Python can't mock datetime.date. We would get the error like below:

TypeError: can't set attributes of built-in/extension type 'datetime.date'

It is verbose in a strange way.

I am under the impression that Python wants to be terse and expressive. That's what people have been saying about Python. But, a while back, I encountered a problem where I wanted to get the first element of a list or get None. It turns out that there's no satisfied solution to solve this task.

This thread offers like 5 ways of getting first element or None. I have no idea why Python doesn't include a method like this in its standard library.

On this point, I do feel Ruby is much more expressive, terse, and beautiful.

Give it a kudos