Webdev: the times, they are a changin'

Levi Pearson levipearson at gmail.com
Thu Apr 19 11:27:17 MDT 2012


On Thu, Apr 19, 2012 at 7:13 AM, Dave Smith <dave at thesmithfam.org> wrote:

> That is my biggest beef with Python: You simply cannot auto-indent. And before you say, "Well, the language naturally indents, so there's no need", may I offer this preemptive retort:

The meaning of 'auto-indent' is a bit different in Python than it is
with languages that do not consider the amount of indentation to be
lexically significant.  When your language has non-whitespace block
delimiters, then whitespace can be fiddled with trivially without
changing the program meaning.  You can indent and un-indent without
looking at the context, so this is what most text editors do.  The
fact that this fails with Python does not mean that Python indentation
can't be easily understood and refactored by a program.

Python's language reference describes how whitespace is tokenized into
'indent' and 'dedent' tokens, which correspond to 'block open' and
'block close' tokens in other languages.  It's very simple and easy to
understand, so it is not a difficult problem to take a correct python
program and perform transformations on it based on its token stream
and end up with a correct program afterward, at least as far as block
delimiting goes.

> If I have an existing block of code that I want to wrap in an "if" statement, I have to manually indent the whole block (thank goodness for Vim's Ctrl+v and Shift+i). In C++, I would simply wrap the block in an "if" with its curly braces, and then auto-indent using Vim's "%" curly brace matching command and the "=" auto-indent command.
>
> If I attempt the same with Python, I'm likely to change the flow control in the block. Blarg!

I'm sorry that vim sucks at editing Python code.  This is not Python's
fault, though.

> I've come to two conclusions about writing Python code:
>
> 1. Pylint is not optional.
> 2. Unit tests with 100% code coverage are not optional.
>
> Those two commandments are necessary just to give you the ground level confidence that your code is syntactically valid and free from even the most basic errors (like using a name before its assigned, nonexistent module, and even some syntax errors, for example).

Python is no different in this respect than most run-time type checked
languages. If you don't rely on poorly-implemented editor
functionality to do your coding, I think you are less likely to
introduce subtle errors in your code with Python than, say,
Javascript.

        --Levi


More information about the PLUG mailing list