Luke Lee

Software Engineer

Web + Desktop + Science

Fork me on Github

Decorating main

I've read a few articles about decorating the main function in a Python script.

These are interesting ideas, but some of the article comments point out several problems with the decorator approach. The biggest problem to me is that the decorator executes the actual decorated function as soon as it's encountered.

I've always thought the if __name__ == "__main__": syntax was ugly.

I remember writing Python for years without every really knowing what it even meant. To me it was just the Python way of 'int main()' in C and a weird Python idiom.

I finally started to understand what was really happening behind the scenes of that weird idiom when I began playing around with metaprogramming [1]. However, I still think this is a strange syntax for beginners and subtly exposes them to an introspection concept very early on.

This syntax is truly some of the worst Python has to offer and oddly enough it's one of the first that new developers see.

[1] Great talk on metaprogramming

Published: 06-04-2012 17:59:00