Roads of Code

Travels in the Land of Computer Programming

Let Every Pixel Count – Drop def and class Keywords from Python

In the spirit of letting every pixel count, why do we not drop the def and class keywords from Python? I am not proposing it, but just wondering about it.

Python has discarded the braces {} and the semicolons as used in the C-programming language, but has added the def keyword. In C, a function is defined like this:

    x = 74;
    y = 86;
    return x + y;

In Python, like this:

def add_numbers():
    x = 74
    y = 86
    return x + y

Why not copy that good idea from C and define it like this in Python:

    x = 74
    y = 86
    return x + y

Of course, there would be the problem of how to distinguish between class definitions and function definitions. One solution would be to make the format of the class and function names significant for the interpreter, e.g. the interpreter could require that:

  • classes be written in PascalCase,┬áthat
  • funtions be written in camelCase, and that
  • variables┬ábe written in lowercase with words separated by underscores.

To distinguish between function and variable names that have only one word, function names should always end with a capital letter.

Here is an example:

PoliceDog(): # Class
    hair_color = 'black' # Variable with two words in name

    findContraband(): #Function
        return True

    barK(): # Function with one word in name
        return 'noise'

The difference between a statement with a function definition header and a function call is the presence or absence of the colon.