Table of Contents
Why must the main function be forced as the entry point?
Home Backend Development Python Tutorial What is the reason why Python does not have a main function?

What is the reason why Python does not have a main function?

Apr 19, 2023 am 10:01 AM
python main

Why must the main function be forced as the entry point?

These languages ​​are all compiled languages, and the code needs to be compiled into an executable binary file. In order for the operating system/bootloader to find the beginning of the program, such a function needs to be defined.

In short, a crucial beginning needs to be defined in a large amount of executable code.

It is not difficult to see that for these languages, the main function is an indispensable component.

However, when we turn our attention to Python, we will find that the situation is quite different.

  • Python is an interpreted language, a scripting language. The running process is performed from top to bottom, line by line, which means that its starting point is known.

  • Each .py file is an executable file that can be used as the entry file for the entire program, which means that the entry of the program is very flexible and does not need to follow any conventions.

  • Sometimes there is no need to specify the entry file when running a Python project (the command line is more common, such as "python -m http.server 8000"), possibly because there is main. py file, executed as a "file" in the software package.

In short, Python as a scripting language is different from a compiled language. Whether it is a single module (i.e. .py file), or a software package composed of multiple modules, Python can choose a flexible execution method, which is completely unlike other languages ​​​​that must define entry points.

In other words, Python does not require programmers to define a unified entry in syntax (whether it is a function, class or something else).

Some students may be confused because they often see or write the following code:

# main file  
def main():      
    ……  
  
if __name__ == '__main__':      
    main()
Copy after login

Isn’t this the main function of Python? I believe many people think so!

No, not really.

Except that the function name is "main", this code has nothing to do with the main function we introduced earlier. This function is neither necessary nor can it determine the execution order of the program. Even if there is no main function like the above, there will not be any syntax problems.

The main reason why people want to write a main function is actually to emphasize that this is a main function and hope to artificially set it as the first function to be executed.

They may think that the function with this name is easier to remember.

The reason why they want to write name ==‘main’ may be because they want to indicate that main() only executes the current script directly. Run it only if it is imported into other modules, but not when it is imported into other modules.

However, I personally do not recommend this way of writing.

To give a simple example, suppose there are only a few dozen lines of code, or a script file implements a simple function (a crawler, or drawing a turtle, etc.), but all in accordance with Written in the previous way.

It is not recommended to write if name == 'main’, because:

  • First of all, if there is only one file, then this file cannot May be exported.

  • Secondly, if there are multiple files, it is strongly recommended not to write this statement in the entry file (main.py). In theory, its contents should not be exported for use by other modules, since it is the starting point.

  • Finally, in the case of multiple files, it is not recommended to write this statement in a non-entry file, because the most that this statement can do is write some tests. code. Even so, the test code should be written separately to a dedicated directory or file.

I feel sick every time I see this clumsy code. Why write such an if statement? You shouldn't wrap this code into a function at all!

The above is the detailed content of What is the reason why Python does not have a main function?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can the Python interpreter be deleted in Linux system? Can the Python interpreter be deleted in Linux system? Apr 02, 2025 am 07:00 AM

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

How to solve the problem of Pylance type detection of custom decorators in Python? How to solve the problem of Pylance type detection of custom decorators in Python? Apr 02, 2025 am 06:42 AM

Pylance type detection problem solution when using custom decorator In Python programming, decorator is a powerful tool that can be used to add rows...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Apr 02, 2025 am 06:27 AM

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Do FastAPI and aiohttp share the same global event loop? Do FastAPI and aiohttp share the same global event loop? Apr 02, 2025 am 06:12 AM

Compatibility issues between Python asynchronous libraries In Python, asynchronous programming has become the process of high concurrency and I/O...

What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? Apr 02, 2025 am 07:12 AM

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

How to ensure that the child process also terminates after killing the parent process via signal in Python? How to ensure that the child process also terminates after killing the parent process via signal in Python? Apr 02, 2025 am 06:39 AM

The problem and solution of the child process continuing to run when using signals to kill the parent process. In Python programming, after killing the parent process through signals, the child process still...

See all articles