# python中main函数作用

在Python程序中，经常可以看到如下程序结构

```python
def main():
    # my code here

if __name__ == "__main__":
    main()
```

或者

```python
import sys

def main(argv):
    # My code here
    pass

if __name__ == "__main__":
    main(sys.argv)
```

为什么不是直接定义自己的函数，类似

```python
def my_function()
    # my code here

def my_function_two()
    # my code here

# some code
# call function
# print(something)
```

这是因为

* 如果不使用`main`函数，代码就会在导入的时候作为一个模块直接运行
* 其他语言（如C和Java)都有一个`main()`函数在程序执行时调用，使用 `if` 结构，则更为熟悉
* 定义了较为清晰和易读的代码

## 参考

* [Why use def main()?](http://stackoverflow.com/questions/4041238/why-use-def-main)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/develop/python/startup/main_function.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
