Hacker News new | comments | show | ask | jobs | submit login

From your link:

It is this environment in which the idiomatic “conditional script” stanza causes a script to run

The conditional script stanza is optional, and intended to make a .py file function as both a script and a module (i.e. be importable). To me, it's unclear why you would want to do that. The only thing you have to do to avoid using __main__ is not require any file to be both a script and a module/library. Then just put any entry-point code at the top level of whatever script invokes your program. Unfortunately the __main__ stanza is pretty widely used though.

On the topic of modules, there is a wrinkle in requiring __init__.py to be present in any library subdirectory that you want to be part of the namespace. It would be nice to have a more intuitive way for that.




Oh, I do it all the time. It allows me to write a script that I can just run from the command line when I want, like 'fetch_new_images'.

I especially love it when I'm first coding, cause I like to test as I go. That allows me to run it to see what works and what doesn't.

When I'm finished, I know that I can just plop that file into a directory and use it as a library afterward. The majority of the code that I write implements that, and I find it a godsend, personally.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: