Python: meaning of __init__ and __main__
if __name__ == “__main__” do?
When the Python interpreter reads a source file, it executes all of the code found in it. Before executing the code, it will define a few special variables. For example, if the python interpreter is running that module (the source file) as the main program, it sets the special name variable to have a value “main”. If this file is being imported from another module, name will be set to the module’s name.
In the case of your script, let’s assume that it’s executing as the main function, e.g. you said something like
on the command line. After setting up the special variables, it will execute the import statement and load those modules. It will then evaluate the def block, creating a function object and creating a variable called myfunction that points to the function object. It will then read the if statement and see that name does equal “main”, so it will execute the block shown there.
One of the reasons for doing this is that sometimes you write a module (a .py file) where it can be executed directly. Alternatively, it can also be imported and used in another module. By doing the main check, you can have that code only execute when you want to run the module as a program and not have it execute when someone just wants to import your module and call your functions themselves.
Files name init.py are used to mark directories on disk as Python package directories. If you have the files
And mydir is on your path, you can import the code in module.py as
import spam.module or from spam import module
If you remove the init.py file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail.
The init.py file is usually empty, but can be used to export selected portions of the package under more convenient name, hold convenience functions, etc. Given the example above, the contents of the init module can be accessed as
blog comments powered by Disqus