originally published 4/15/2017
As you might expect of an aspiring data scientist, I’ve been learning Python. One of the conventions I have found rather strange is the presence of the
main function in Python modules. Other languages such as C and C++ require a
main function (well, in most situations, anyway). Python doesn’t require a
main function, so I was a bit perplexed as to why I kept running across it.
A common construct in Python is the following
If you were to run my_file.py from the command line like so
then the interpreter would set
name to “main”. The interpreter does this whenever you run a module (i.e. a file) directly. If module_a is imported into module_b, however, the
name variable for module_a is set to its file name.
All of your modules should have their statements wrapped up in a main function. If statements in a module are free-floating then even when that module is imported, those statements will be executed.
When you run
python conventional.py, the statement “I’ve been called” is printed. In short, using the
main function in your modules allows you to have modules that can be both executed directly or imported into other modules. Note that it isn’t required that your main function be called “main”. This is also perfectly acceptable: