Using the Python Interpreter as a Reference

DevPlayer devplayer at gmail.com
Mon Nov 28 19:54:08 EST 2011


On Nov 27, 6:55 pm, Steven D'Aprano <steve
+comp.lang.pyt... at pearwood.info> wrote:
> On Sun, 27 Nov 2011 14:21:01 -0800, Travis Parks wrote:
> > Personally, I find a lot of good things in Python. I thinking tabs are
> > out-of-date. Even the MAKE community wishes that the need for tabs would
> > go away and many implementations have done just that.
>
> Tabs have every theoretical advantage and only one practical
> disadvantage: the common toolsets used by Unix programmers are crap in
> their handling of tabs, and instead of fixing the toolsets, they blame
> the tabs.
>
> The use of spaces as indentation is a clear case of a technically worse
> solution winning over a better solution.
>
> > I have been
> > seriously debating about whether to force a specific number of spaces,
> > such as the classic 4, but I am not sure yet. Some times, 2 or even 8
> > spaces is appropriate (although I'm not sure when).
>
> Why on earth should your language dictate the width of an indentation? I
> can understand that you might care that indents are consistent within a
> single source code unit (a file?), but anything more than that is just
> obnoxious.
>

I do not understand why the interpreter preprocesses each logical line
of source code using something as simple as this:

def reindent( line, spaces_per_tab=os.spaces_per_tab):

    index = 0  # index into line of text
    indent = 0 # increase 1 when in next tab column
    spaces = 0 # index into current column

    for c in line:
        if c == ' ':
            spaces +=1
            if spaces >= spaces_per_tab:
                indent += 1
                spaces = 0
        if c == '\t':   # jump to next tab column
            indent += 1
            spaces = 0
        if c <> ' ' and c <> '\t':
            break
        index += 1
    rest_of_line = line[index:]
    new_indent = ' ' * indent * spaces_per_tab + ' ' * spaces
    newline = new_indent + rest_of_line

    return newline

or even some regex equivelent.

That function would need to be run on each line of source code, after
processing the line continuation character and single/double/triple
quote pairs are processed but before the code block tokenizer (INDENT/
DEDENT) if possible. Unless some of you expert parser/compiler writers
know fancier tricks.

To me, I would think the interpreter finding the coder's intended
indent wouldn't be that hard. And just make the need for consistant
spaces or tabs irrevelent simply by reformatting the indent as
expected. Pretty much all my text editors can.



More information about the Python-list mailing list