An exploration of why Python doesn't require a 'main' function

栏目: IT技术 · 发布时间: 5年前

Many languages start running your program by calling a function of yours that must have a specific name. In C (and many C derived languages), this is just called main() ; in Go, it's main.main() (the main() function in the main package). Python famously doesn't require any such function, and won't automatically call a function called main() even if you create it. Recently I read Why doesn’t Python have a main function? ( via ), which puts forward one discussion for why this is so. However, I have a somewhat different way of explaining this situation.

The core reason that Python doesn't require a main() function is a combination of its execution model (specifically for what happens when you import something) and that under normal circumstances you start Python programs by (implicitly) importing a single file of Python code. So let's look at each of these parts.

In many languages things like functions, classes, and so on are created (defined) by the interpreter or compiler as it parses the source file. In Python, this is not quite the case; instead, def and class are executable statements , and they define classes and functions when they execute (among other things, this is part ofwhy metaclasses work). When Python imports something, it simply executes everything in the file (or the import more generally). When what's executed is def and class statements, you get functions and classes. When what's executed is regular code, you get more complicated things happening, including conditional imports or calling functions on the fly under the right conditions. Or you can write an entire program that just runs inline, as the file is imported.

(This has some interesting consequences, including what reloading a Python module really does .)

However, Python is not quite as unique here as it might look. Many languages have some facility to run arbitrary code early on as the program is 'loading', before the program starts normal execution (Go has init() functions, for example). Where Python is different from these languages is that Python normally starts a program by loading and executing a specific single file. Because Python is only executing a single file, it's unambiguous what code is run in what order and it's straightforward for the code in that file to control what happens. In a sense, rather than picking an arbitrarily named function for where execution (nominally) starts, Python is able to sneakily pick an arbitrarily named file by having you provide it.

(Compiled languages traditionally have a model where code from a bunch of separate files is all sort of piled up together. In Python, you can't really aggregate multiple files together into a shared namespace this way; one way or another, you have to import them and everything starts from some initial file.)

Where this nice model breaks down and needs a workaround is if you run a package with ' python -m ... ', where Python doesn't really have a single file that you're executing (or it'd have to make __init__.py serve double duty). As covered in the official documentation's __main__ — Top-level script environment ( via ), Python adopts the arbitrary convention of loading a __main__.py file from your package and declaring it more or less the point where execution starts.

(Under at least some situations, your package's __init__.py may also be executed.)

PS: contrary to the original article's views , I strongly suggest that you have a main() function , because there are significant benefits to keeping your program importable .


很遗憾的说,推酷将在这个月底关闭。人生海海,几度秋凉,感谢那些有你的时光。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C++程序设计原理与实践

C++程序设计原理与实践

(美)Bjarne Stroustrup / 王刚 等 / 机械工业出版社 / 2010.7 / 108.00元

本书是经典程序设计思想与C++开发实践的完美结合,是C++之父回归校园后对C++编程原理和技巧的全新阐述。书中全面地介绍了程序设计基本原理,包括基本概念、设计和编程技术、语言特性以及标准库等,教你学会如何编写具有输入、输出、计算以及简单图形显示等功能的程序。此外,本书通过对C++思想和历史的讨论、对经典实例(如矩阵运算、文本处理、测试以及嵌入式系统程序设计)的展示,以及对C语言的简单描述,为你呈现......一起来看看 《C++程序设计原理与实践》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码