Wednesday, August 08, 2007

Brace yourself

While doing my usual rounds of Python hacking, a colleague commented how weird Python code looked as it didn't have any braces (for the uninitiated, the Python interpreter identifies control flow using indentation levels). Naturally, I smirked about it and explained how Python is different & how it helped make the code more clean. However, to be honest, I myself didn't have much clue as to why had the Python designers made such a choice. So after some late night Googling, I found this:

"One of Python's controversial features, using indentation level rather than begin/end or braces, was driven by this philosophy: since there are no braces, there are no style wars over where to put the braces. Interestingly, Lisp has exactly the same philosophy on this point: everyone uses emacs to indent their code, so they don't argue over the indentation."

- Peter Norvig, "Python for Lisp Programmers".

Moral: Good languages & good editors go together.

2 comments:

Paddy3118 said...

Actually it goes a little further: in languages where braces are used, their can often be subtle bugs where the codes indentation does not match the braces i.e. the braces say that a statement belongs in one place, but the indentation makes it seem to belong in another place. All programs with braces (or bracing constructs), need to have their indentation match those bracing constructs to aid in the correct reading of the code. In Python there can be no confusion as their are no braces!
If it is indented correctly then what you see grouped visually is indeed the block structure of the program.

- Paddy.

Manas Pathak said...

I guess so, but my motivation for writing this post is to indicate how people think being different from the prototype is weird.