Movatterモバイル変換


[0]ホーム

URL:


Loading video player…

Defining Main Functions in Python: Overview

Many programming languages have a special function that is automatically executed when an operating system starts to run a program. This function is usually calledmain() and must have a specific return type and arguments according to the language standard. On the other hand, the Python interpreter executes scripts starting at the top of the file, and there is no specific function that Python automatically executes.

Nevertheless, having a defined starting point for the execution of a program is useful for understanding how a program works. Python programmers have come up with several conventions to define this starting point.

By the end of this course, you’ll understand:

  • What the special__name__ variable is and how Python defines it
  • Why you would want to use amain() in Python
  • What conventions there are for definingmain() in Python
  • What the best practices are for what code to put into yourmain()
Download

Sample Code (.zip)

2.2 KB
Download

Course Slides (.pdf)

277.0 KB

00:00Hi, I’m Rich Bibby with realpython.com.Many programming languages have a special function that is automaticallyexecuted when an operating system starts to run a program.

00:10This function is usually calledmain() and must have a specific returntype and arguments according to the language standard. On the other hand,the Python interpreter executes scripts starting at the top of the file,and there is no specific function that Python automatically executes.

00:28Nevertheless,having a defined starting point for the execution of a program is useful forunderstanding how a program works.Python programmers have come up with several conventions to define this startingpoint. By the end of this tutorial,you’ll understand what this special__name__ variable is and how Python defines it,why you would want to use amain() function in Python,what conventions there are for definingmain() in Python, and what the best practicesare for what code to put into yourmain() functions.

01:01So, let’s get started. In some Python scripts,you may see a function definition and a conditional statement that looks likethis. In this code, there is a function calledmain() that prints the phrase"Hello World!" when the Python interpreter executes it.

01:16There is also a conditional, orif statement, that checks the value of__name__ andcompares it to the string"__main__". When theif statement evaluates toTrue, the Python interpreter executesmain().

01:29If you want to learn more about conditional statements,then you can find more tutorials on the Real Python site—just enter “conditional statements” in the search box.

01:39This code pattern is quite common in Python files that you want to be executedas a script or imported into another module. To help understand how this codewill execute,you should first understand how the Python interpreter sets__name__, and that’swhat’s coming up in the next lesson,where you will see that it depends on how the code is being executed.

Become a Member to join the conversation.

Course Contents

Overview
20%

[8]ページ先頭

©2009-2026 Movatter.jp