Python Tutorial: if __name__ == "__main__" for beginners

Опубликовано: 22 Июнь 2024
на канале: Ferds Tech Channel
433
14

In this video, I will cover the construct if _name_ == "__main__": in Python.

The construct if _name_ == "__main__": is often referred to as the "main guard" or the "main block" in Python.
These terms are interchangeable and commonly used in the Python community to describe the if _name_ == "__main__": construct.

What is __name__?

In Python, every module (a file containing Python code) has a special built-in variable called __name__.
When a module is run directly, the interpreter sets _name_ to "__main__".
If the module is imported into another module, _name_ is set to the module's name.

Why use if _name_ == "__main__"?

1. if _name_ == "__main__" is used to check if a script is being run directly or being imported.
2. This allows developers to write code that only executes when the script is run directly, but not when it is imported elsewhere.

#python #pythontutorial #pythonforbeginners


На этой странице сайта вы можете посмотреть видео онлайн Python Tutorial: if __name__ == "__main__" for beginners длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Ferds Tech Channel 22 Июнь 2024, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 433 раз и оно понравилось 14 зрителям. Приятного просмотра!