CodeX
Published in

CodeX

if __name__ == “__main__”, What It Does & Why We Need It

# How this prevents unwanted code from accidentally running

cool art

If you’ve programmed in Python for a while, you most probably have seen the if __name__ == "__main__" statement in a couple of projects. This article aims to explain in simple english what this statement does, and why we need it.

The __name__ variable

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Liu Zuo Lin

Software Engineer, Python Tutor, Tech Writer. My Free Ebooks — 1) Python Zero To One 2) 40 Python Practice Questions For Beginners — https://zlliu.co/books