---
title: "Python基础与应用"
author: "Perrin Yong"
author_profile: https://www.pystone.net/profile/
published_by: "Perrin Yong"
canonical: https://www.pystone.net/notes/python-fundamentals-and-applications/
type: note
content_role: unspecified
visibility: public
id_stability: rename-stable
source_path: "10-计算机、信息技术与工程/03-软件工程与质量保障/CI-CD与质量保障/CI-CD工具链/Python基础与应用.md"
content_hash: 03a60b4fb7ff99ae05e1803a7fb3c15d4773dec50a33a25f539b3b9eaddf8660
knowledge_version: 224c990773de.5fa8af6e39fa
site_commit: 224c990773de166d23a886306577dd90379529ce
notes_commit: 5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a
---
# Python基础与应用

> 创建时间：2020/12/23 17:36

  * Python基础与应用
    * 基本运行机制
      * 关于
      * 关于sys.path
      * 类与self
    * 命令行参数
    * 应用
      * 修改文件
    * Ref

## 基本运行机制

而 Python属于脚本语言，动态的逐行解释运行。也就是从脚本第一行开始运行，没有统一的入口。使用缩进对齐组织代码的执行，所有没有缩进的代码（非函数定义和类定义），都会在载入时自动执行。

### 关于

`__name__`,`__main__.py`&`__init__.py`

  * 当脚本作为单个文件直接运行是，`__name__`为`'__main__'`

  * 当脚本作为模块导入使用时，`__name__`为这个模块的名字

`if __name__ == '__main__'`则表示当前脚本直接执行，而非被其他模块调用时，运行后面的代码。

> 这为测试驱动开发提供了极好的支持，我们可以在每个模块中写上测试代码，这些测试代码仅当模块被Python直接执行时才会运行，代码和测试完美的结合在一起。

假设有如下一个包

```cpp
string str1 = &quot;test&quot;;
string str2 = &quot;test&quot;;

```

> 关于`__init__.py`
>  如果目录中包含了 `__init__.py` 时，当用 import 导入该目录时，会执行 `__init__.py` 里面的代码。

目录中所有 py 文件的内容都为：

`print(__name__)`

在a文件夹的父文件夹中

  * 执行命令：python -c “import a.b.c”，输出结果：

```cpp
string str1 = &quot;test&quot;;
string str2 = &quot;test&quot;;

```

作为模块导入，`__name__`为这个模块的名字，导入时，`__init__.py`中的代码会被执行

  * 执行命令：python -m a.b，输出结果：

```cpp
string str1 = &quot;test&quot;;
string str2 = &quot;test&quot;;

```

b作为一个模块被执行，`__init__.py`中的代码会被执行，模块中的`__main__.py`作为入口执行，其中的`__name__`参数值为`__main__`

  * 执行命令：python a\b，输出结果：

```cpp
string str1 = &quot;test&quot;;
string str2 = &quot;test&quot;;

```

执行a\b这个包，即运行a\b这个包的`__main__.py`，不会执行`__init__.py`,其中`__main__.py`的`__name__`参数值为`__main__`

### 关于sys.path

sys.path中的第一个元素为当前工作路径。
脚本作为一个模块运行时，即执行`python -m package`，sys.path的第一个参数为执行命令行的路径。当直接执行脚本时，sys.path的第一个参数为相应脚本的路径。

### 类与self

```cpp
string str1 = &quot;test&quot;;
string str2 = &quot;test&quot;;

```

![Alt text](/media/145b6da2f397d26c744d.png)

## 命令行参数

  * `-c command` \- 执行 command 中的 Python 代码。

  * `-m <module-name>` \- 在 sys.path 中搜索指定名称的模块并将其内容作为 **main** 模块来执行。

使用 sys 的 sys.argv 来获取命令行参数：
sys.argv[0] 表示脚本名，args[1:]后面的才是真正命令行输入的参数。

```cpp
string str1 = &quot;test&quot;;
string str2 = &quot;test&quot;;

```

> getopt模块是专门处理命令行参数的模块，用于获取命令行选项和参数，也就是sys.argv。更多参见: <https://blog.csdn.net/ouyang_peng/article/details/79390920>

## 应用

### 修改文件

修改某一行

```cpp
string str1 = &quot;test&quot;;
string str2 = &quot;test&quot;;

```

* * *

替换字符串，直接写入源文件

```csharp
string a = &quot;test&quot;;
string tmp = &quot;est&quot;;
string b = &quot;t&quot; + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

```

* * *

写入新文件后再替换源文件

```csharp
string a = &quot;test&quot;;
string tmp = &quot;est&quot;;
string b = &quot;t&quot; + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

```

* * *

正则表达式 re 的应用

```csharp
string a = &quot;test&quot;;
string tmp = &quot;est&quot;;
string b = &quot;t&quot; + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

```

输出

```csharp
string a = &quot;test&quot;;
string tmp = &quot;est&quot;;
string b = &quot;t&quot; + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

```
```csharp
string a = &quot;test&quot;;
string tmp = &quot;est&quot;;
string b = &quot;t&quot; + tmp;
Console.WriteLine(string .ReferenceEquals(str1, str2));

```

## Ref

<https://www.cnblogs.com/lvxiuquan/archive/2012/07/09/2582437.html>
