{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/python-fundamentals-and-applications/",
  "atlas": "https://www.pystone.net/?node=python-fundamentals-and-applications#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/python-fundamentals-and-applications.md",
  "context": "https://www.pystone.net/notes/python-fundamentals-and-applications.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:python-fundamentals-and-applications",
  "slug": "python-fundamentals-and-applications",
  "title": "Python基础与应用",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "Python基础与应用",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "03a60b4fb7ff99ae05e1803a7fb3c15d4773dec50a33a25f539b3b9eaddf8660",
  "assets": [
    {
      "reference": "assets/1608718131552.png",
      "url": "/media/145b6da2f397d26c744d.png",
      "mediaType": "image/png",
      "contentHash": "145b6da2f397d26c744db7f03e39d55bdf5d8c5f5bb45bbe4e3549de61036045",
      "byteLength": 2035,
      "width": 237,
      "height": 64
    }
  ],
  "headings": [
    {
      "depth": 1,
      "text": "Python基础与应用",
      "anchor": "python基础与应用",
      "citation": "https://www.pystone.net/notes/python-fundamentals-and-applications/#python%E5%9F%BA%E7%A1%80%E4%B8%8E%E5%BA%94%E7%94%A8"
    },
    {
      "depth": 2,
      "text": "基本运行机制",
      "anchor": "基本运行机制",
      "citation": "https://www.pystone.net/notes/python-fundamentals-and-applications/#%E5%9F%BA%E6%9C%AC%E8%BF%90%E8%A1%8C%E6%9C%BA%E5%88%B6"
    },
    {
      "depth": 3,
      "text": "关于",
      "anchor": "关于",
      "citation": "https://www.pystone.net/notes/python-fundamentals-and-applications/#%E5%85%B3%E4%BA%8E"
    },
    {
      "depth": 3,
      "text": "关于sys.path",
      "anchor": "关于syspath",
      "citation": "https://www.pystone.net/notes/python-fundamentals-and-applications/#%E5%85%B3%E4%BA%8Esyspath"
    },
    {
      "depth": 3,
      "text": "类与self",
      "anchor": "类与self",
      "citation": "https://www.pystone.net/notes/python-fundamentals-and-applications/#%E7%B1%BB%E4%B8%8Eself"
    },
    {
      "depth": 2,
      "text": "命令行参数",
      "anchor": "命令行参数",
      "citation": "https://www.pystone.net/notes/python-fundamentals-and-applications/#%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0"
    },
    {
      "depth": 2,
      "text": "应用",
      "anchor": "应用",
      "citation": "https://www.pystone.net/notes/python-fundamentals-and-applications/#%E5%BA%94%E7%94%A8"
    },
    {
      "depth": 3,
      "text": "修改文件",
      "anchor": "修改文件",
      "citation": "https://www.pystone.net/notes/python-fundamentals-and-applications/#%E4%BF%AE%E6%94%B9%E6%96%87%E4%BB%B6"
    },
    {
      "depth": 2,
      "text": "Ref",
      "anchor": "ref",
      "citation": "https://www.pystone.net/notes/python-fundamentals-and-applications/#ref"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:software-engineering-and-quality",
      "title": "软件工程与质量保障",
      "url": "https://www.pystone.net/notes/software-engineering-and-quality/",
      "atlas": "https://www.pystone.net/?node=software-engineering-and-quality#knowledge-atlas",
      "label": "软件工程与质量保障",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "CI-CD与质量保障/CI-CD工具链中的“Python基础与应用”导航项",
      "citation": "https://www.pystone.net/notes/software-engineering-and-quality/#ci-cd%E4%B8%8E%E8%B4%A8%E9%87%8F%E4%BF%9D%E9%9A%9Cci-cd%E5%B7%A5%E5%85%B7%E9%93%BE"
    }
  ],
  "contentMarkdown": "# Python基础与应用\n\n> 创建时间：2020/12/23 17:36\n\n  * Python基础与应用\n    * 基本运行机制\n      * 关于\n      * 关于sys.path\n      * 类与self\n    * 命令行参数\n    * 应用\n      * 修改文件\n    * Ref\n\n## 基本运行机制\n\n而 Python属于脚本语言，动态的逐行解释运行。也就是从脚本第一行开始运行，没有统一的入口。使用缩进对齐组织代码的执行，所有没有缩进的代码（非函数定义和类定义），都会在载入时自动执行。\n\n### 关于\n\n`__name__`,`__main__.py`&`__init__.py`\n\n  * 当脚本作为单个文件直接运行是，`__name__`为`'__main__'`\n\n  * 当脚本作为模块导入使用时，`__name__`为这个模块的名字\n\n`if __name__ == '__main__'`则表示当前脚本直接执行，而非被其他模块调用时，运行后面的代码。\n\n> 这为测试驱动开发提供了极好的支持，我们可以在每个模块中写上测试代码，这些测试代码仅当模块被Python直接执行时才会运行，代码和测试完美的结合在一起。\n\n假设有如下一个包\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n> 关于`__init__.py`\n>  如果目录中包含了 `__init__.py` 时，当用 import 导入该目录时，会执行 `__init__.py` 里面的代码。\n\n目录中所有 py 文件的内容都为：\n\n`print(__name__)`\n\n在a文件夹的父文件夹中\n\n  * 执行命令：python -c “import a.b.c”，输出结果：\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n作为模块导入，`__name__`为这个模块的名字，导入时，`__init__.py`中的代码会被执行\n\n  * 执行命令：python -m a.b，输出结果：\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\nb作为一个模块被执行，`__init__.py`中的代码会被执行，模块中的`__main__.py`作为入口执行，其中的`__name__`参数值为`__main__`\n\n  * 执行命令：python a\\b，输出结果：\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n执行a\\b这个包，即运行a\\b这个包的`__main__.py`，不会执行`__init__.py`,其中`__main__.py`的`__name__`参数值为`__main__`\n\n### 关于sys.path\n\nsys.path中的第一个元素为当前工作路径。\n脚本作为一个模块运行时，即执行`python -m package`，sys.path的第一个参数为执行命令行的路径。当直接执行脚本时，sys.path的第一个参数为相应脚本的路径。\n\n### 类与self\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n![Alt text](assets/1608718131552.png)\n\n## 命令行参数\n\n  * `-c command` \\- 执行 command 中的 Python 代码。\n\n  * `-m <module-name>` \\- 在 sys.path 中搜索指定名称的模块并将其内容作为 **main** 模块来执行。\n\n使用 sys 的 sys.argv 来获取命令行参数：\nsys.argv[0] 表示脚本名，args[1:]后面的才是真正命令行输入的参数。\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n> getopt模块是专门处理命令行参数的模块，用于获取命令行选项和参数，也就是sys.argv。更多参见: <https://blog.csdn.net/ouyang_peng/article/details/79390920>\n\n## 应用\n\n### 修改文件\n\n修改某一行\n\n```cpp\nstring str1 = &quot;test&quot;;\nstring str2 = &quot;test&quot;;\n\n```\n\n* * *\n\n替换字符串，直接写入源文件\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n* * *\n\n写入新文件后再替换源文件\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n* * *\n\n正则表达式 re 的应用\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n输出\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n## Ref\n\n<https://www.cnblogs.com/lvxiuquan/archive/2012/07/09/2582437.html>\n"
}
