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.

Level: intermediateTopic: Python advanced

__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.

Before you go

Take the cheat sheet with you

One free PDF: the key AI-engineering terms, formulas and Python idioms. No spam; unsubscribe any time.