Python indentation

Mahrt, Dallas dallasm at aiinet.com
Wed Jul 7 12:10:26 EDT 2004


> 
> Can you specify what desirable construction Python is denying you? Or 
> are you talking about freedom in the abstract?
> 
> Cheers,
> Brian

For the sake of discussion I have run into a case where Python's
indenting has caused added complexity to a solution. I was writing a
quick JSP-like engine using Python instead of Java. In JSP you could
perform an iteration of a page element (rows in a table for example)
using something like the following...

    <?  
        for(int i=0; i < collection.length(); i++)
        { 
    ?>

        <tr><td></td></tr>

    <?  } 
    ?>

Since the Java has explicit scope markers {}, when the code is generated
from this page it will be properly scope

        for (int i=0; i < collection.length(); i++)
        {
        print '<tr><td></td></tr>'
        }

In Python, this isn't as easy. We should not require that the contents
of the XML CDATA elements have significant whitespace as this makes the
code very unreadable and rather difficult to write. Instead I had to
explicitly define scopes in the Python code so that the engine could
generate the correct indents in the generated code. Example

    <psp:scope>
        <psp:scopeCode>
            for i in range(len(collection)):
        </psp:scopeCode>
        
        <tr><td></tr></td>

    </psp:scope>

While this is a reasonable workaround, it is a lot more difficult than
if there were scope delimiters in the language. 


FYI: Besides this one problem, I actually prefer the use of significant
whitespace.

---
Dallas S. Mahrt	Software Engineer	
Applied Innovation Inc.    
<disclaimer>The views and opinions above are mine and do not reflect
those of my employer</disclaimer>





More information about the Python-list mailing list