程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Why doesnt Python have a main function? Why dont I recommend writing the main function?

編輯:Python

without doubt Python There is no such thing as main Entry function , But I often see some articles on the Internet “Python Of main function ”、“ Suggest to write main function ”……

Some people know it , His intention may be to imitate those authentic main function , But a lot of people are obviously misled ( Or I misunderstood ), It's very cumbersome code to write .

In this issue “Python Why? ” Let's talk about it Python Why not? main function ?

Before we get to the point , Let's first answer these two questions : So-called “main function ” What is it? ? Why do some programming languages have to force a main function ?

Some programming languages use main Function as the execution entry of the program , for example C/C++、C#、 Java、Go and Rust etc. , They have a specific meaning :

  • main Function names are mandatory , That is to say, there must be a main function
  • main There can only be one function at most , In other words, the entry to the program is unique
  • There are certain requirements for grammatical format , With a relatively fixed template

Why force a main The entry function ?

These are compiled languages , You need to compile the code into executable binaries , To make the operating system / The starter finds the starting point of the program , So we have to agree on this function . In short , It's in a lot of code , You need to define a significant start for execution .

It's not hard to see. ,main Functions are an important and indispensable part of those languages .

However , Let's see Python, The situation is very different .

  • Python It's interpreted language , Script language , The running process is from top to bottom , Run line by line , That is to say, its starting point is knowable
  • Every .py A file is an executable file , Can be used as the entry file of the whole program , In other words, the entry of the program is flexible , There is no agreement that must be observed
  • Sometimes run Python project , There is no entry file specified ( More common on the command line , for example "python -m http.server 8000"), It could be there __main__.py file , The bag it's in is treated like a “ file ” Here we go

It comes down to , mean Python This scripting language is different from compiled languages , It doesn't matter at the single module level ( That is, a .py file ), Or at the package level of multiple modules , You can choose flexible execution methods , Unlike other languages, they can't execute without the agreed entry .

in other words ,Python There is no need to specify at the syntax level that programmers must define a unified entry ( Whether it's a function or a class or something ).

Some students may have doubts , Because they often see or write the following code themselves :

# main There's some body code in it
def main():
……
if __name__ == '__main__':
main()

Isn't that Python Of main Function ? I believe many students will think so !

no ! no !

Except that the function name is “main” outside , It's the same as the orthodox main Functions have nothing to do with half a cent , There is no compulsion , Nor does it necessarily determine the sequence of program execution . It's missing , It won't lead to any grammar problems .

The reason why some insiders want to name one ”main“ function , Actually, I want to emphasize it ” The main “ status , Want to artificially arrange it as the first function to execute . They might think of a function named like this , It's easier to remember .

The reason why some insiders want to write if name == '__main__' , May want to show main() Run only if the current script is executed directly , Do not want to be imported into other modules to run .

For these “ insider ”, They have a point .

however , I personally don't recommend it , And sometimes it's very disgusting !

The most obvious example : There are only dozens of lines of code , Or just one script file , Implement a simple function ( A little bit of a reptile 、 use turtle Draw a picture and so on ), But they're all written in the previous style .

Every time I see this kind of unthinkable and cumbersome code , I feel sick . Why write that line if Sentence? ? If possible , It should be split main function , It doesn't even have to be encapsulated as a function !

I personally sum up the following experience :

  • Break the habit of thinking , Write authentic code .main Entry functions are specific to some languages , It's not supposed to be here Python in “ Copy sth. without catching its spirit ”, You should understand the characteristics of scripting languages , Write a simple and elegant style
  • Use main.py Instead of main(). because Python The execution unit of a program is actually a script file , Not a function or class , So it is suggested to name the entry file as main.py, Internal functions depend on demand
  • If you can , Use __main__.py As an entry file . This file combines the “-m” Parameters use , Very easy to use .
  • It is not recommended to write if name == '__main__'. First , If there's only one file , Because there is no possibility of export , Not recommended . secondly , When there are multiple files , Entrance file (main.py) It is not recommended to write this sentence , The code logic of this file should be refined , In theory, its content should not be exported to other modules for use , Because it's the starting point ! Last , It is not recommended to write multi file non entry files , Because this judgment is written in the non entry file , The biggest function is to write some test code , But the test code should be separated , Write to a special directory or file .

Summary : This paper first explains what is main Entry function , And why some languages are forced to write main function ; next , Explained why Python No need to write main function ; Finally, it is aimed at some people's habitual misunderstanding , I share four personal programming experiences .

The above is all the content shared this time , Want to know more python Welcome to official account :Python Programming learning circle , send out “J” Free access to , Daily dry goods sharing


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved