A neat trick for handling indents in Python

Anyone who has been working with Python knows that its rigid indentation rules can only do so much in the way of improving readability. However, with increasing code complexity, one can easily get lost without knowing their position in the “indent tree”. Some IDE’s give some sort of guidance bars on the side of the text hinting at the indent level you’re at. Moreover, strong indentation is good for reading the code, not necessarily a good thing when writing and testing.




But I am a vim user, and after a few years learning and harvesting the power of vim, one does not simply forfeit vim for an IDE. On top of this, I often program over the net and through a series of firewall, making the use of more advanced IDE’s somewhat unpractical. So there is a neat little trick I use. Let’s say I’m writing a function that must iterate over a series of compounds in a data base. For each compound I want it to collect some data, make a few calculations and then plot some data for each compound. The code starts like this:

def myAwesomeModel(myArg):
if(myArg != "Valid Option"):
print("ERROR: Your argument is invalid")
return(1)
for compound in compoundList:
name=getName(compound)
for method in methodsList:
for property in interestingPropertiesList:
(...)
Continue reading “A neat trick for handling indents in Python”