diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 041196e..0000000 --- a/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -__pycache__/ -.vscode/settings.json -.md/ -读我.txt -temp.py -test.py -*.pyc -*.zip \ No newline at end of file diff --git a/README.md b/README.md index 8b057d6..e69de29 100644 --- a/README.md +++ b/README.md @@ -1,20 +0,0 @@ -## 說明 - -這裏的程式碼被用於一個 `Python` 參考說明,你可以訪問 [PYTHON 參考說明](https://learnscript.net/zh-hant/python-reference/),檢視 `Python` 相關的文章。 - -每個 `.py` 程式碼檔案都含有其對應文章的資訊。 - -## 關於 - -這些 `Python` 的文章可能還會不斷補充,您可以訂閱 [學腳本](https://learnscript.net/zh-hant/)。 - - -## 说明 - -这里的代码被用于一个 `Python` 参考说明,你可以访问 [PYTHON 参考说明](https://learnscript.net/zh/python-reference/),查看 `Python` 相关的文章。 - -每个 `.py` 代码文件都含有其对应文章的信息。 - -## 关于 - -这些 `Python` 的文章可能还会不断补充,您可以关注 [学脚本](https://learnscript.net/zh/)。 diff --git a/src/zh-hant/import_system/add_zip.py b/src/zh-hant/import_system/add_zip.py deleted file mode 100644 index f594adf..0000000 --- a/src/zh-hant/import_system/add_zip.py +++ /dev/null @@ -1,15 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/import-system/module-search-path/set-module-search-path/ 如何設定 Python 模組搜尋路徑 -''' - -# 取得壓縮檔案 plants.zip 的絕對路徑 -import os -import sys -zip_path = os.path.abspath('plants.zip') - -# 將 plants.zip 的絕對路徑新增至模組搜尋路徑 -sys.path.append(zip_path) - -# 匯入 plants.zip 中的 tree 模組 -import tree \ No newline at end of file diff --git a/src/zh-hant/import_system/apple.py b/src/zh-hant/import_system/apple.py deleted file mode 100644 index 42a798e..0000000 --- a/src/zh-hant/import_system/apple.py +++ /dev/null @@ -1 +0,0 @@ -print('模組 apple 被尋找器發現了!') \ No newline at end of file diff --git a/src/zh-hant/import_system/finders.py b/src/zh-hant/import_system/finders.py deleted file mode 100644 index 1ee0944..0000000 --- a/src/zh-hant/import_system/finders.py +++ /dev/null @@ -1,15 +0,0 @@ -# 嘗試找到名稱包含 PathFinder 的尋找器 -import sys -path_finder = None - -for finder in sys.meta_path: - if 'PathFinder' in str(finder): - path_finder = finder - break - -# 將找到的 PathFinder 尋找器刪除 -if path_finder: - sys.meta_path.remove(path_finder) - -# 匯入 apple 模組 -import apple \ No newline at end of file diff --git a/src/zh-hant/import_system/my.pth b/src/zh-hant/import_system/my.pth deleted file mode 100644 index f25daca..0000000 --- a/src/zh-hant/import_system/my.pth +++ /dev/null @@ -1,3 +0,0 @@ -my_modules -# 這是一行註解哦! -import sys;sys.path.append('custom_lib') \ No newline at end of file diff --git a/src/zh-hant/import_system/my.reg b/src/zh-hant/import_system/my.reg deleted file mode 100644 index d6d8571..0000000 --- a/src/zh-hant/import_system/my.reg +++ /dev/null @@ -1,4 +0,0 @@ -Windows Registry Editor Version 5.00 - -[HKEY_CURRENT_USER\Software\Python\PythonCore\3.12\PythonPath] -@="C:\\;custom_lib" \ No newline at end of file diff --git a/src/zh-hant/import_system/os.py b/src/zh-hant/import_system/os.py deleted file mode 100644 index bcae2ee..0000000 --- a/src/zh-hant/import_system/os.py +++ /dev/null @@ -1 +0,0 @@ -print('我不是那個 os 模組!') diff --git a/src/zh-hant/import_system/plants/tree.py b/src/zh-hant/import_system/plants/tree.py deleted file mode 100644 index 57ff2a6..0000000 --- a/src/zh-hant/import_system/plants/tree.py +++ /dev/null @@ -1,6 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/import-system/module-search-path/set-module-search-path/ 如何設定 Python 模組搜尋路徑 -''' - -print('哦?這是一顆樹?') \ No newline at end of file diff --git a/src/zh-hant/import_system/print_getcwd.py b/src/zh-hant/import_system/print_getcwd.py deleted file mode 100644 index 7c5b24b..0000000 --- a/src/zh-hant/import_system/print_getcwd.py +++ /dev/null @@ -1,8 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/import-system/module-search-path/relative-paths-in-module-search-path/ 如何在 Python 模組搜尋路徑中使用相對路徑 -''' - -# 顯示當前工作目錄 -import os -print(os.getcwd()) \ No newline at end of file diff --git a/src/zh-hant/import_system/print_meta_path.py b/src/zh-hant/import_system/print_meta_path.py deleted file mode 100644 index 73d788b..0000000 --- a/src/zh-hant/import_system/print_meta_path.py +++ /dev/null @@ -1,3 +0,0 @@ -# 顯示元路徑尋找器 -import sys -print(sys.meta_path) \ No newline at end of file diff --git a/src/zh-hant/import_system/print_path.py b/src/zh-hant/import_system/print_path.py deleted file mode 100644 index a6ed711..0000000 --- a/src/zh-hant/import_system/print_path.py +++ /dev/null @@ -1,8 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/import-system/module-search-path/ 什麽是 Python 模組搜尋路徑 -''' - -# 顯示 sys.path 表示的 Python 搜尋路徑 -import sys -print(sys.path) \ No newline at end of file diff --git a/src/zh-hant/import_system/print_prefix.py b/src/zh-hant/import_system/print_prefix.py deleted file mode 100644 index b8e65a5..0000000 --- a/src/zh-hant/import_system/print_prefix.py +++ /dev/null @@ -1,9 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/import-system/module-search-path/directories-in-module-search-path/ Python 模組搜尋路徑中的目錄有哪些 -''' - -# 顯示標準/擴充程式庫目錄 -import sys -print(sys.prefix) -print(sys.exec_prefix) \ No newline at end of file diff --git a/src/zh-hant/import_system/python._pth b/src/zh-hant/import_system/python._pth deleted file mode 100644 index 38e585e..0000000 --- a/src/zh-hant/import_system/python._pth +++ /dev/null @@ -1,7 +0,0 @@ -# 標準程式庫 zip 檔案路徑 -python312.zip -# 標準/擴充程式庫目錄 -. - -# 新增第三方套件目錄,並使目錄中的 pth 檔案可以被正常處理 -import site \ No newline at end of file diff --git a/src/zh-hant/import_system/run.py b/src/zh-hant/import_system/run.py deleted file mode 100644 index ddd8b29..0000000 --- a/src/zh-hant/import_system/run.py +++ /dev/null @@ -1,8 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/import-system/module-search-path/directories-in-module-search-path/ Python 模組搜尋路徑中的目錄有哪些 -''' - -# 顯示 sys.path 的第一個元素,他是該腳本檔案所在的目錄 -import sys -print(f'腳本位於:{sys.path[0]}') \ No newline at end of file diff --git a/src/zh-hant/import_system/set_path.py b/src/zh-hant/import_system/set_path.py deleted file mode 100644 index 2667f76..0000000 --- a/src/zh-hant/import_system/set_path.py +++ /dev/null @@ -1,15 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/import-system/module-search-path/set-module-search-path/ 如何設定 Python 模組搜尋路徑 -''' - -# 取得資料夾 plants 的絕對路徑 -import os -import sys -plants_path = os.path.abspath('plants') - -# 將資料夾 plants 的絕對路徑新增至模組搜尋路徑 -sys.path.append(plants_path) - -# 直接匯入 plants 中的 tree 模組 -import tree \ No newline at end of file diff --git a/src/zh-hant/import_system/zip/__main__.py b/src/zh-hant/import_system/zip/__main__.py deleted file mode 100644 index 305f68e..0000000 --- a/src/zh-hant/import_system/zip/__main__.py +++ /dev/null @@ -1,8 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/import-system/module-search-path/directories-in-module-search-path/ Python 模組搜尋路徑中的目錄有哪些 -''' - -# 顯示 sys.path 的第一個元素,他是 __main__.py 所在 zip 檔案的路徑 -import sys -print(f'__main__.py 位於:{sys.path[0]}') \ No newline at end of file diff --git a/src/zh-hant/modules/cache_clear.py b/src/zh-hant/modules/cache_clear.py deleted file mode 100644 index 6bd71ff..0000000 --- a/src/zh-hant/modules/cache_clear.py +++ /dev/null @@ -1,22 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/modules/module-cache/ 什麽是 Python 模組快取 -''' - -import sys - -# 首次匯入模組 car,並建立 Car 物件 -import car -module1 = sys.modules['car'] -car1 = car.Car() - -# 清除模組 car 的快取 -del sys.modules['car'] - -# 再次匯入模組 car,並建立 Car 物件 -import car -module2 = sys.modules['car'] -car2 = car.Car() - -print(f'module1 == module2 能成立?{module1 == module2}') -print(f'car1 與 car2 的型別相同?{type(car1) == type(car2)}') \ No newline at end of file diff --git a/src/zh-hant/modules/cache_none.py b/src/zh-hant/modules/cache_none.py deleted file mode 100644 index dfe3d99..0000000 --- a/src/zh-hant/modules/cache_none.py +++ /dev/null @@ -1,11 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/modules/module-cache/ 什麽是 Python 模組快取 -''' - -import sys -# 模組 car 的完整名稱為 car,這裏提前將其快取設定為 None -sys.modules['car'] = None - -# 匯入模組 car 將導致例外狀況 -import car diff --git a/src/zh-hant/modules/car.py b/src/zh-hant/modules/car.py deleted file mode 100644 index b1f1528..0000000 --- a/src/zh-hant/modules/car.py +++ /dev/null @@ -1,8 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/modules/module-cache/ 什麽是 Python 模組快取 -''' - -# 一個表示汽車的類別 -class Car: - pass diff --git a/src/zh-hant/modules/homework/__init__.py b/src/zh-hant/modules/homework/__init__.py deleted file mode 100644 index 8ccd4b9..0000000 --- a/src/zh-hant/modules/homework/__init__.py +++ /dev/null @@ -1,9 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/modules/module-fully-qualified-names/ 什麽是 Python 模組的完整名稱 -''' - -print('homework 套件!') - -# 這裏的完整名稱基於 student.py 所在的目錄 -import homework.english diff --git a/src/zh-hant/modules/homework/english.py b/src/zh-hant/modules/homework/english.py deleted file mode 100644 index 3077ce6..0000000 --- a/src/zh-hant/modules/homework/english.py +++ /dev/null @@ -1,6 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/modules/module-fully-qualified-names/ 什麽是 Python 模組的完整名稱 -''' - -print('english 模組!') \ No newline at end of file diff --git a/src/zh-hant/modules/student.py b/src/zh-hant/modules/student.py deleted file mode 100644 index 70d41f2..0000000 --- a/src/zh-hant/modules/student.py +++ /dev/null @@ -1,14 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/modules/module-fully-qualified-names/ 什麽是 Python 模組的完整名稱 -''' - -# 執行 student.py 可以正常匯入 teacher,因為 student.py 所在的目錄將被新增至搜尋路徑 -import teacher - -# homework 套件也可以被正常匯入 -import homework - -import sys -import types -print(isinstance(sys.modules["sys"], types.ModuleType)) \ No newline at end of file diff --git a/src/zh-hant/modules/teacher.py b/src/zh-hant/modules/teacher.py deleted file mode 100644 index 23bb998..0000000 --- a/src/zh-hant/modules/teacher.py +++ /dev/null @@ -1,6 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/modules/module-fully-qualified-names/ 什麽是 Python 模組的完整名稱 -''' - -print('teacher 模組!') \ No newline at end of file diff --git a/src/zh-hant/packages/hero/__init__.py b/src/zh-hant/packages/hero/__init__.py deleted file mode 100644 index c972df2..0000000 --- a/src/zh-hant/packages/hero/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/packages/ 什麽是 Python 套件,常規套件,命名空間套件?有何不同 -''' - -# hero 是一個常規套件 -print('我是常規套件 hero!') diff --git a/src/zh-hant/packages/my_plants.py b/src/zh-hant/packages/my_plants.py deleted file mode 100644 index 7caefcb..0000000 --- a/src/zh-hant/packages/my_plants.py +++ /dev/null @@ -1,15 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/packages/ 什麽是 Python 套件,常規套件,命名空間套件?有何不同 -''' - -# 將命令列切換至該腳本所在目錄,然後使用 Python 執行該腳本 -import os -import sys - -# 取得壓縮檔案 plants.zip 的絕對路徑,並新增至模組搜尋路徑 -zip_path = os.path.abspath('plants.zip') -sys.path.append(zip_path) - -# 匯入 plants.zip 中的 flowers 套件 -import flowers \ No newline at end of file diff --git a/src/zh-hant/packages/my_school.py b/src/zh-hant/packages/my_school.py deleted file mode 100644 index 246fae4..0000000 --- a/src/zh-hant/packages/my_school.py +++ /dev/null @@ -1,7 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/packages/submodules-and-subpackages/ 什麽是 Python 子模組和子套件 -''' - -# 匯入套件 school 的子套件 homework,他是一個常規套件 -import school.homework diff --git a/src/zh-hant/packages/plants/flowers/__init__.py b/src/zh-hant/packages/plants/flowers/__init__.py deleted file mode 100644 index 89ca975..0000000 --- a/src/zh-hant/packages/plants/flowers/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/packages/ 什麽是 Python 套件,常規套件,命名空間套件?有何不同 -''' - -# flowers 是一個常規套件 -print('我是套件 flowers!') \ No newline at end of file diff --git a/src/zh-hant/packages/print_sitepackages.py b/src/zh-hant/packages/print_sitepackages.py deleted file mode 100644 index 70e1764..0000000 --- a/src/zh-hant/packages/print_sitepackages.py +++ /dev/null @@ -1,8 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/packages/third-party-packages/ 什麽是 Python 第三方套件 -''' - -# 通過 site 模組取得第三方套件目錄 -import site -print(site.getsitepackages()) diff --git a/src/zh-hant/packages/school/homework.py b/src/zh-hant/packages/school/homework.py deleted file mode 100644 index e965595..0000000 --- a/src/zh-hant/packages/school/homework.py +++ /dev/null @@ -1,6 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/packages/submodules-and-subpackages/ 什麽是 Python 子模組和子套件 -''' - -print('這裏是模組 homework!') \ No newline at end of file diff --git a/src/zh-hant/packages/school/homework/__init__.py b/src/zh-hant/packages/school/homework/__init__.py deleted file mode 100644 index b3b4acc..0000000 --- a/src/zh-hant/packages/school/homework/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -''' -本節文章 -https://learnscript.net/zh-hant/python-reference/packages/submodules-and-subpackages/ 什麽是 Python 子模組和子套件 -''' - -print('功課太多了!') \ No newline at end of file diff --git a/src/zh/import_system/add_zip.py b/src/zh/import_system/add_zip.py deleted file mode 100644 index b17e61f..0000000 --- a/src/zh/import_system/add_zip.py +++ /dev/null @@ -1,15 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/import-system/module-search-path/set-module-search-path/ 如何设置 Python 模块搜索路径 -''' - -# 获取压缩文件 plants.zip 的绝对路径 -import os -import sys -zip_path = os.path.abspath('plants.zip') - -# 将 plants.zip 的绝对路径添加至模块搜索路径 -sys.path.append(zip_path) - -# 导入 plants.zip 中的 tree 模块 -import tree \ No newline at end of file diff --git a/src/zh/import_system/apple.py b/src/zh/import_system/apple.py deleted file mode 100644 index 65f6938..0000000 --- a/src/zh/import_system/apple.py +++ /dev/null @@ -1 +0,0 @@ -print('模块 apple 被查找器发现了!') \ No newline at end of file diff --git a/src/zh/import_system/finders.py b/src/zh/import_system/finders.py deleted file mode 100644 index 2471509..0000000 --- a/src/zh/import_system/finders.py +++ /dev/null @@ -1,15 +0,0 @@ -# 尝试找到名称包含 PathFinder 的查找器 -import sys -path_finder = None - -for finder in sys.meta_path: - if 'PathFinder' in str(finder): - path_finder = finder - break - -# 将找到的 PathFinder 查找器删除 -if path_finder: - sys.meta_path.remove(path_finder) - -# 导入 apple 模块 -import apple \ No newline at end of file diff --git a/src/zh/import_system/my.pth b/src/zh/import_system/my.pth deleted file mode 100644 index fe80249..0000000 --- a/src/zh/import_system/my.pth +++ /dev/null @@ -1,3 +0,0 @@ -my_modules -# 这是一行注释哦! -import sys;sys.path.append('custom_lib') \ No newline at end of file diff --git a/src/zh/import_system/my.reg b/src/zh/import_system/my.reg deleted file mode 100644 index d6d8571..0000000 --- a/src/zh/import_system/my.reg +++ /dev/null @@ -1,4 +0,0 @@ -Windows Registry Editor Version 5.00 - -[HKEY_CURRENT_USER\Software\Python\PythonCore\3.12\PythonPath] -@="C:\\;custom_lib" \ No newline at end of file diff --git a/src/zh/import_system/os.py b/src/zh/import_system/os.py deleted file mode 100644 index 65bfc22..0000000 --- a/src/zh/import_system/os.py +++ /dev/null @@ -1 +0,0 @@ -print('我不是那个 os 模块!') diff --git a/src/zh/import_system/plants/tree.py b/src/zh/import_system/plants/tree.py deleted file mode 100644 index e882cd1..0000000 --- a/src/zh/import_system/plants/tree.py +++ /dev/null @@ -1,6 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/import-system/module-search-path/set-module-search-path/ 如何设置 Python 模块搜索路径 -''' - -print('哦?这是一颗树?') \ No newline at end of file diff --git a/src/zh/import_system/print_getcwd.py b/src/zh/import_system/print_getcwd.py deleted file mode 100644 index 322cc9d..0000000 --- a/src/zh/import_system/print_getcwd.py +++ /dev/null @@ -1,8 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/import-system/module-search-path/relative-paths-in-module-search-path/ 如何在 Python 模块搜索路径中使用相对路径 -''' - -# 显示当前工作目录 -import os -print(os.getcwd()) \ No newline at end of file diff --git a/src/zh/import_system/print_meta_path.py b/src/zh/import_system/print_meta_path.py deleted file mode 100644 index 3f99c0d..0000000 --- a/src/zh/import_system/print_meta_path.py +++ /dev/null @@ -1,3 +0,0 @@ -# 显示元路径查找器 -import sys -print(sys.meta_path) \ No newline at end of file diff --git a/src/zh/import_system/print_path.py b/src/zh/import_system/print_path.py deleted file mode 100644 index eae3554..0000000 --- a/src/zh/import_system/print_path.py +++ /dev/null @@ -1,8 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/import-system/module-search-path/ 什么是 Python 模块搜索路径 -''' - -# 显示 sys.path 表示的 Python 搜索路径 -import sys -print(sys.path) \ No newline at end of file diff --git a/src/zh/import_system/print_prefix.py b/src/zh/import_system/print_prefix.py deleted file mode 100644 index 66717d4..0000000 --- a/src/zh/import_system/print_prefix.py +++ /dev/null @@ -1,9 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/import-system/module-search-path/directories-in-module-search-path/ Python 模块搜索路径中的目录有哪些 -''' - -# 显示标准/扩展库目录 -import sys -print(sys.prefix) -print(sys.exec_prefix) \ No newline at end of file diff --git a/src/zh/import_system/python._pth b/src/zh/import_system/python._pth deleted file mode 100644 index 906331c..0000000 --- a/src/zh/import_system/python._pth +++ /dev/null @@ -1,7 +0,0 @@ -# 标准库 zip 文件路径 -python312.zip -# 标准/扩展库目录 -. - -# 添加第三方包目录,并使目录中的 pth 文件可以被正常处理 -import site \ No newline at end of file diff --git a/src/zh/import_system/run.py b/src/zh/import_system/run.py deleted file mode 100644 index 1a64a7a..0000000 --- a/src/zh/import_system/run.py +++ /dev/null @@ -1,8 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/import-system/module-search-path/directories-in-module-search-path/ Python 模块搜索路径中的目录有哪些 -''' - -# 显示 sys.path 的第一个元素,他是该脚本文件所在的目录 -import sys -print(f'脚本位于:{sys.path[0]}') \ No newline at end of file diff --git a/src/zh/import_system/set_path.py b/src/zh/import_system/set_path.py deleted file mode 100644 index 15daee1..0000000 --- a/src/zh/import_system/set_path.py +++ /dev/null @@ -1,15 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/import-system/module-search-path/set-module-search-path/ 如何设置 Python 模块搜索路径 -''' - -# 获取文件夹 plants 的绝对路径 -import os -import sys -plants_path = os.path.abspath('plants') - -# 将文件夹 plants 的绝对路径添加至模块搜索路径 -sys.path.append(plants_path) - -# 直接导入 plants 中的 tree 模块 -import tree \ No newline at end of file diff --git a/src/zh/import_system/zip/__main__.py b/src/zh/import_system/zip/__main__.py deleted file mode 100644 index 8c5f44c..0000000 --- a/src/zh/import_system/zip/__main__.py +++ /dev/null @@ -1,8 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/import-system/module-search-path/directories-in-module-search-path/ Python 模块搜索路径中的目录有哪些 -''' - -# 显示 sys.path 的第一个元素,他是 __main__.py 所在 zip 文件的路径 -import sys -print(f'__main__.py 位于:{sys.path[0]}') \ No newline at end of file diff --git a/src/zh/modules/cache_clear.py b/src/zh/modules/cache_clear.py deleted file mode 100644 index ed84031..0000000 --- a/src/zh/modules/cache_clear.py +++ /dev/null @@ -1,22 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/modules/module-cache/ 什么是 Python 模块缓存 -''' - -import sys - -# 首次导入模块 car,并创建 Car 对象 -import car -module1 = sys.modules['car'] -car1 = car.Car() - -# 清除模块 car 的缓存 -del sys.modules['car'] - -# 再次导入模块 car,并创建 Car 对象 -import car -module2 = sys.modules['car'] -car2 = car.Car() - -print(f'module1 == module2 能成立?{module1 == module2}') -print(f'car1 与 car2 的类型相同?{type(car1) == type(car2)}') \ No newline at end of file diff --git a/src/zh/modules/cache_none.py b/src/zh/modules/cache_none.py deleted file mode 100644 index 05bd2a4..0000000 --- a/src/zh/modules/cache_none.py +++ /dev/null @@ -1,11 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/modules/module-cache/ 什么是 Python 模块缓存 -''' - -import sys -# 模块 car 的完全限定名为 car,这里提前将其缓存设置为 None -sys.modules['car'] = None - -# 导入模块 car 将导致异常 -import car diff --git a/src/zh/modules/car.py b/src/zh/modules/car.py deleted file mode 100644 index 909deca..0000000 --- a/src/zh/modules/car.py +++ /dev/null @@ -1,8 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/modules/module-cache/ 什么是 Python 模块缓存 -''' - -# 一个表示汽车的类 -class Car: - pass diff --git a/src/zh/modules/homework/__init__.py b/src/zh/modules/homework/__init__.py deleted file mode 100644 index e509e6d..0000000 --- a/src/zh/modules/homework/__init__.py +++ /dev/null @@ -1,9 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/modules/module-fully-qualified-names/ 什么是 Python 模块的完全限定名 -''' - -print('homework 包!') - -# 这里的完全限定名基于 student.py 所在的目录 -import homework.english diff --git a/src/zh/modules/homework/english.py b/src/zh/modules/homework/english.py deleted file mode 100644 index 020d9b1..0000000 --- a/src/zh/modules/homework/english.py +++ /dev/null @@ -1,6 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/modules/module-fully-qualified-names/ 什么是 Python 模块的完全限定名 -''' - -print('english 模块!') \ No newline at end of file diff --git a/src/zh/modules/student.py b/src/zh/modules/student.py deleted file mode 100644 index a6bcdbd..0000000 --- a/src/zh/modules/student.py +++ /dev/null @@ -1,14 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/modules/module-fully-qualified-names/ 什么是 Python 模块的完全限定名 -''' - -# 运行 student.py 可以正常导入 teacher,因为 student.py 所在的目录将被添加至搜索路径 -import teacher - -# homework 包也可以被正常导入 -import homework - -import sys -import types -print(isinstance(sys.modules["sys"], types.ModuleType)) \ No newline at end of file diff --git a/src/zh/modules/teacher.py b/src/zh/modules/teacher.py deleted file mode 100644 index 736725a..0000000 --- a/src/zh/modules/teacher.py +++ /dev/null @@ -1,6 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/modules/module-fully-qualified-names/ 什么是 Python 模块的完全限定名 -''' - -print('teacher 模块!') \ No newline at end of file diff --git a/src/zh/packages/hero/__init__.py b/src/zh/packages/hero/__init__.py deleted file mode 100644 index ebefd33..0000000 --- a/src/zh/packages/hero/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/packages/ 什么是 Python 包,常规包,命名空间包?有何不同 -''' - -# hero 是一个常规包 -print('我是常规包 hero!') diff --git a/src/zh/packages/my_plants.py b/src/zh/packages/my_plants.py deleted file mode 100644 index 91cae8b..0000000 --- a/src/zh/packages/my_plants.py +++ /dev/null @@ -1,15 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/packages/ 什么是 Python 包,常规包,命名空间包?有何不同 -''' - -# 将命令行切换至该脚本所在目录,然后使用 Python 运行该脚本 -import os -import sys - -# 获取压缩文件 plants.zip 的绝对路径,并添加至模块搜索路径 -zip_path = os.path.abspath('plants.zip') -sys.path.append(zip_path) - -# 导入 plants.zip 中的 flowers 包 -import flowers \ No newline at end of file diff --git a/src/zh/packages/my_school.py b/src/zh/packages/my_school.py deleted file mode 100644 index ca6f8db..0000000 --- a/src/zh/packages/my_school.py +++ /dev/null @@ -1,7 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/packages/submodules-and-subpackages/ 什么是 Python 子模块和子包 -''' - -# 导入包 school 的子包 homework,他是一个常规包 -import school.homework diff --git a/src/zh/packages/plants/flowers/__init__.py b/src/zh/packages/plants/flowers/__init__.py deleted file mode 100644 index f3cf030..0000000 --- a/src/zh/packages/plants/flowers/__init__.py +++ /dev/null @@ -1,7 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/packages/ 什么是 Python 包,常规包,命名空间包?有何不同 -''' - -# flowers 是一个常规包 -print('我是包 flowers!') \ No newline at end of file diff --git a/src/zh/packages/print_sitepackages.py b/src/zh/packages/print_sitepackages.py deleted file mode 100644 index f62a1a1..0000000 --- a/src/zh/packages/print_sitepackages.py +++ /dev/null @@ -1,8 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/packages/third-party-packages/ 什么是 Python 第三方包 -''' - -# 通过 site 模块获取第三方包目录 -import site -print(site.getsitepackages()) diff --git a/src/zh/packages/school/homework.py b/src/zh/packages/school/homework.py deleted file mode 100644 index edd8326..0000000 --- a/src/zh/packages/school/homework.py +++ /dev/null @@ -1,6 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/packages/submodules-and-subpackages/ 什么是 Python 子模块和子包 -''' - -print('这里是模块 homework!') \ No newline at end of file diff --git a/src/zh/packages/school/homework/__init__.py b/src/zh/packages/school/homework/__init__.py deleted file mode 100644 index 786a95c..0000000 --- a/src/zh/packages/school/homework/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -''' -本节文章 -https://learnscript.net/zh/python-reference/packages/submodules-and-subpackages/ 什么是 Python 子模块和子包 -''' - -print('功课太多了!') \ No newline at end of file