What is Pythonen?

Pythonen is python for people with OCD, like me.

Standard python uses indentation to signal where loops and logical structures close. To me, this is like writing a sentence without a full stop That previous sentence doesn't look finished! I can work out where the sentence ended because the next sentence starts with a capital letter, but it just doesn't look right.

Pythonen is exactly the same as python but with a closing 'end' statement for loops and logical structrues.

Other than offending my OCD, there are two major disadvantages of not having the closing statement in standard python.

  1. Indentation has to be done manually and it has to be right or the code won't work correctly, often with no error identified.
  2. If you copy and paste a block of code from somewhere else, you have to manually indent it to the correct level (See 1).

Now, python purists will tell you that this is all worth it, because it forces indentation and makes the code easier to read.

Fair enough, but if you have a closing statement, you can auto-indent the entire code by a single command (e.g. Ctrl-i in Matlab) so there's no real barrier to indenting uniformly and quickly. The addition of the end statement does not make the readability significantly worse for the majority of cases and for programmers used to other languages, it is actually easier to read. It can look a little cumbersome if you have many nested loops, but if that's the case, you should probably redesign your code anyway.

See the example python and pythonen code below. Is it really so much harder to read that it justifies the pain of manual indentation?


# -*- coding: utf-8 -*-
# This is standard python
def vowel_remover(text):
    string = ""
    for l in text:
        if l.lower() != "a" and \
           l.lower() != "e" and \
           l.lower() != "i" and \
           l.lower() != "o" and \
           l.lower() != "u":
            string += l
    return string
    
print vowel_remover("hello world!")

# -*- coding: pyn_ascii -*-
# This is pythonen
def vowel_remover(text):
    string = ""
    for l in text:
        if l.lower() != "a" and \
           l.lower() != "e" and \
           l.lower() != "i" and \
           l.lower() != "o" and \
           l.lower() != "u":
            string += l
        end
    end
    return string
end
print vowel_remover("hello world!")

How Can I Use Pythonen?

Download a few scripts from here.

pythonen2.zip

Place the 'pyn_ascii.py' file in your encodings folder, which is under the following path from your python installation folder.

path_to_python/Lib/encodings

Then to use Pythonen, you just need to add the # -*- coding: pyn_ascii -*- line at the top of your file and your Pythonen will run using the normal python interpreter.

There are a few rules to follow.

1. 'end' becomes a reserved keyword, so shouldn't be used for variable names, etc.

2. Continuation lines must use a backslash as the last character to denote the continuation.

If you use Notepad++ as your editor, you can easily modify the python language under settings/Style Configurator, to add 'end' as a user-defined keyword so that it gets formatted as the other keywords.

You can also install 'PythonScript' (from here) and add the other scripts so that you can use them while editing. I allocate them to the shortcuts shown below.

1. py2pyn.py (Ctrl-Alt-N) convert the standard python script you are editing to Pythonen by adding in the 'end' keywords

2. pyn2py.py (Ctrl-Alt-Y) converts the Pythonen script you are editing to standard Python (removes the 'end' keywords)

3. pynIndent.py (Ctrl-Alt-I) auto-indents your Pythonen, so you can write the script and auto-indent it without ever having to manually indent. This is especially useful if you copy a block of code from somewhere else and want to indent it quickly.

Warning!! This is a hack that I just did for fun (although I do program in pythonen now!).

No warranty given. Use at your own risk.

I might look into modifying cpython to make a proper pythonen interpreter.