# python编程风格

## 模块结构和布局

可以参考以下布局：

1. 起始行(Unix): 通常只有在类 Unix 环境下才使用起始行,有起始行就能够仅输入脚本名字来执行脚本,无 需直接调用解释器。
2. 模块文档: 简要介绍模块的功能及重要全局变量的含义，模块外可通过 `module.__doc__` 访问这些内容。
3. 模块导入: 导入当前模块的代码需要的所有模块;每个模块仅导入一次(当前模块被加载时);函数内部的模块导入代码不会被执行, 除非该函数正在执行。
4. 变量定义: 这里定义的变量为全局变量,本模块中的所有函数都可直接使用。从好的编程风格角度说, 除非必须,否则就要尽量使用局部变量代替全局变量,如果坚持这样做,你的代码就不但容易维护,而且还可以提高性能并节省内存。
5. 类定义: 所有的类都需要在这里定义。当模块被导入时 `class` 语句会被执行, 类也就会被定义。类的文档变量是 `class.__doc__`。
6. 函数定义: 此处定义的函数可以通过`module.function()`在外部被访问到,当模块被导入时`def`语句会被执行, 函数也就都会定义好,函数的文档变量是`function.__doc__`。
7. 主程序: 无论这个模块是被别的模块导入还是作为脚本直接执行,都会执行这部分代码。通常这里不会有太多功能性代码,而是根据执行的模式调用不同的函数。

## 参考

* [Python核心编程](https://book.douban.com/subject/3112503/)
* [Google Python风格指南](http://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/)


---

# 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/python_style.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.
