AI engineering glossary · Python advanced
What is __main__ guard?
__main__ guard (also called if __name__ == '__main__'): The idiom if __name__ == '__main__': that runs code only when a file is executed directly and not when it is imported. It lets a file act as both a reusable module and a script.
__main__ guard: an example
Putting main() under the guard lets tests import functions from the file without launching the whole program.
Related terms
More Python advanced terms
Where you learn it
__main__ guard is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.