Python 物件導向 --- 模組

來源:互聯網
上載者:User

標籤:turn   而在   auth   管理   version   lis   author   code   rac   

目標
  • 模組
  • 包
  • 模組的製作
01,模組1.1 模組的概念

注 模組是Python程式架構的一個核心概念

  • 每一個以副檔名 py 結尾的 python 原始碼檔案都是一個 模組
  • 模組名 同樣也是一個 標識符,需要符合標識符的命名規則
  • 在模組中定義的 全域變數、函數、類 都是提供給外界直接使用的 工具
  • 模組 就好比是 工具包,想要使用這個工具包中的工具,就需要先 匯入 這個模組
1.2 模組的兩種匯入方式1,import 匯入
import 模組名1, 模組名2  (不推薦)

提示:在匯入模組時,每個匯入應該獨佔一行

import 模組名1import 模組名2
  • 匯入後
    • 通過 模組名. 使用 模組提供的工具 - - 全域變數、函數、類
使用 as 指定模組的別名

注: 如果模組的名字太長,可以使用 as 指定模組的名稱,以方便在代碼中使用

import 模組名1 as 模組別名

注意: 模組別名 應該符合 大駝峰命名法

2, from ... import 匯入
  • 如果希望 從某一個模組 中,匯入 部分 工具, 就可以使用 from ... import 的方式
  • import 模組名 是 一次性 把模組中 所有工具全部匯入,並且通過 模組名/別名 訪問
# 從 模組 匯入 某一個工具from 模組名1 import 工具名
  • 匯入後
    • 不需要 通過 模組名
    • 可以直接使用 模組提供的工具 - - 全域變數、函數、類

注意
如果 兩個模組,存在 同名的函數,那麼 後匯入模組的函數,會 覆蓋先前置入的函數

  • 開發時 import 代碼應該統一寫在 代碼的頂部,更容易及時發現衝突
  • 一旦發生衝突,可以使用 as 關鍵字 給其中一個工具啟一個別名
from...import *
# 從 模組 匯入 所有工具from 模組名1 import *

注意
這種方式不推薦使用,因為函數重名並沒有任何的提示,出現問題不好排查

1.3 模組的搜尋順序

python 的解譯器在 匯入模組 時,會:
1,搜尋 目前的目錄 指定模組名的檔案,如果有就直接匯入
2,如果沒有,再搜尋 系統目錄

注:在開發中,給檔案起名,不要和 系統模組檔案 重名

python 中每一個模組都是一個內建屬性 __file__ 可以 查看模組 的 完整路徑

樣本

import random# 產生一個 0 ~ 10 的數字rand = random.randint(0, 10)print(rand)

注意: 如果目前的目錄下,存在一個 random.py 的檔案,程式就無法正常執行了

  • 這個時候, python 的解譯器會 載入目前的目錄 下的 random.py 而不會載入 系統的 `random 模組
1.4 原則 - - 每一個檔案都應該可以被匯入
  • 一個 獨立的 python 檔案 就是一個 模組
  • 在匯入檔案時,檔案中 所有沒有任何縮排的代碼 都會被執行一遍
實際開發情境
  • 在實際開發中,每一個模組都是獨立開發的,大多都有專人負責
  • 開發人員 通常會在 模組下方 增加一些測試代碼
    • 僅在模組內使用,而被匯入到其它檔案中不需要執行
name 屬性
  • __name__ 屬性可以做到,測試模組的代碼 只在測試情況下被運行,而在 匯入時不會被執行
  • __name__ 是 python 的一個內建屬性,記錄著一個 字串
  • 如果 是被其它檔案匯入的,__name__ 就是 模組名
  • 如果 是當前執行的程式,__name__ 是 __main__
在很多 python 檔案中都會看到以下格式的代碼:
# 匯入模組# 定義全域變數# 定義類# 定義函數# 在代碼的最下方def main():    # 測試代碼...    pass# 根據 __name__ 判斷是否執行下方代碼if __name__ == "__main__":    main()
02,包(package)概念
  • 包 是一個 包含多個模組 的 特殊目錄
  • 目錄下有一個 特殊的檔案 __init__.py
  • 包名的 命名方式 和 變數名 一致,小寫字母 + _

好處

  • 使用 import 包名 可以一次性匯入 包 中 所有的模組
案例演練

1,建立一個 hm_message 的包
2,在目錄下,建立兩個檔案 send_message 和 receive_message
3,在 send_message 檔案中定義一個 send 函數
4,在 receive_message 檔案中定一個 receive 函數
5,在外部直接匯入 hm_message 的包

init.py
  • 要在外界使用 包 中的模組,需要在 __init__.py 中指定 對外界提供的模組列表
# 從 目前的目錄 匯入 模組列表from . import send_messagefrom . import receive_message
def receive():    return "這是來自 110 的簡訊"
def send(text):    print("正在發送 %s" % text)
03,發布模組
  • 如果希望自己開發的模組,分享 給其它人,可以按照以下步驟操作
3.1 製作發布壓縮包步驟

目錄結構

[[email protected] python_code]# tree.├── hm_message│   ├── __init__.py│   ├── receive_message.py│   └── send_message.py└── setup.py
1,建立 setup.py
  • setup.py 的檔案
[[email protected] python_code]# more setup.pyfrom distutils.core import setupsetup(name="String.hm_message", # 包名    version="1.0", # 版本    description="itheima‘s 發送和接收訊息模組", #描述資訊    long_description="完整的發送和接收訊息模組", #完整描述資訊    author="itheima", # 作者    author_email="[email protected]", # 作者郵件    url="www.itheima.com", # 首頁    py_modules=["hm_message.send_message", "hm_message.receive_message"])
2,構建模組
[[email protected] python_code]# python3 setup.py buildrunning buildrunning build_pycreating buildcreating build/libcreating build/lib/hm_messagecopying hm_message/__init__.py -> build/lib/hm_messagecopying hm_message/send_message.py -> build/lib/hm_messagecopying hm_message/receive_message.py -> build/lib/hm_message[[email protected] python_code]# tree.├── build│   └── lib│       └── hm_message│           ├── __init__.py│           ├── receive_message.py│           └── send_message.py├── hm_message│   ├── __init__.py│   ├── receive_message.py│   └── send_message.py└── setup.py4 directories, 7 files
3,產生發布壓縮包
[[email protected] python_code]# python3 setup.py sdistrunning sdistrunning checkwarning: sdist: manifest template ‘MANIFEST.in‘ does not exist (using default file list)warning: sdist: standard file not found: should have one of README, README.txt, README.rstwriting manifest file ‘MANIFEST‘creating String.hm_message-1.0creating String.hm_message-1.0/hm_messagemaking hard links in String.hm_message-1.0...hard linking setup.py -> String.hm_message-1.0hard linking hm_message/__init__.py -> String.hm_message-1.0/hm_messagehard linking hm_message/receive_message.py -> String.hm_message-1.0/hm_messagehard linking hm_message/send_message.py -> String.hm_message-1.0/hm_messagecreating distCreating tar archiveremoving ‘String.hm_message-1.0‘ (and everything under it)[[email protected] python_code]# tree.├── build│   └── lib│       └── hm_message│           ├── __init__.py│           ├── receive_message.py│           └── send_message.py├── dist│   └── String.hm_message-1.0.tar.gz├── hm_message│   ├── __init__.py│   ├── receive_message.py│   └── send_message.py├── MANIFEST└── setup.py5 directories, 9 files

注意 要製作那個版本的模組,就使用哪個版本的解譯器執行

3.2 安裝模組
[[email protected] dist]# tar zxvf String.hm_message-1.0.tar.gzString.hm_message-1.0/String.hm_message-1.0/PKG-INFOString.hm_message-1.0/hm_message/String.hm_message-1.0/hm_message/__init__.pyString.hm_message-1.0/hm_message/receive_message.pyString.hm_message-1.0/hm_message/send_message.pyString.hm_message-1.0/setup.py[[email protected] dist]# tree.├── String.hm_message-1.0│   ├── hm_message│   │   ├── __init__.py│   │   ├── receive_message.py│   │   └── send_message.py│   ├── PKG-INFO│   └── setup.py└── String.hm_message-1.0.tar.gz2 directories, 6 files[[email protected] dist]# cd String.hm_message-1.0[[email protected] String.hm_message-1.0]# python3 setup.py installrunning installrunning buildrunning build_pycreating buildcreating build/libcreating build/lib/hm_messagecopying hm_message/__init__.py -> build/lib/hm_messagecopying hm_message/send_message.py -> build/lib/hm_messagecopying hm_message/receive_message.py -> build/lib/hm_messagerunning install_libcreating /usr/local/python3/lib/python3.7/site-packages/hm_messagecopying build/lib/hm_message/__init__.py -> /usr/local/python3/lib/python3.7/site-packages/hm_messagecopying build/lib/hm_message/send_message.py -> /usr/local/python3/lib/python3.7/site-packages/hm_messagecopying build/lib/hm_message/receive_message.py -> /usr/local/python3/lib/python3.7/site-packages/hm_messagebyte-compiling /usr/local/python3/lib/python3.7/site-packages/hm_message/__init__.py to __init__.cpython-37.pycbyte-compiling /usr/local/python3/lib/python3.7/site-packages/hm_message/send_message.py to send_message.cpython-37.pycbyte-compiling /usr/local/python3/lib/python3.7/site-packages/hm_message/receive_message.py to receive_message.cpython-37.pycrunning install_egg_infoWriting /usr/local/python3/lib/python3.7/site-packages/String.hm_message-1.0-py3.7.egg-info[[email protected] String.hm_message-1.0]# cd /usr/local/python3/lib/python3.7/site-packages/[[email protected] site-packages]# tree hm_message/hm_message/├── __init__.py├── __pycache__│   ├── __init__.cpython-37.pyc│   ├── receive_message.cpython-37.pyc│   └── send_message.cpython-37.pyc├── receive_message.py└── send_message.py1 directory, 6 files[[email protected] site-packages]# ipython3Python 3.7.0 (default, Jul 18 2018, 16:50:30)Type ‘copyright‘, ‘credits‘ or ‘license‘ for more informationIPython 6.4.0 -- An enhanced Interactive Python. Type ‘?‘ for help.In [1]: import hm_messageIn [2]: hm_message.send_message.send("hello")正在發送 helloIn [3]: hm_message.receive_message.receive()Out[3]: ‘這是來自 110 的簡訊‘
卸載模組

直接從安裝目錄下,把安裝模組的 目錄 刪除就可以

[[email protected] String.hm_message-1.0]# cd /usr/local/python3/lib/python3.7/site-packages/[[email protected] site-packages]# rm -f hm_message*
3.3 pip 安裝第三方模組
  • 第三方模組 通常是指由 知名的第三方團隊 開發的 並且被 **程式員廣泛使用的 python 包/模組
    • 例如 pygame 就是一套非常成熟的 遊戲開發模組
  • pip 是一個現代的,通用的 python 包管理工具
  • 提供了對 python 包的尋找、下載、安裝、卸載等功能
安裝和卸載命令入下:
# 將模組安裝到 Python 2.x 環境$ sudo pip install pygame$ sudo pip uninstall pygame# 將模組安裝到 Python 3.x 環境$ sudo pip3 install pygame$ sudo pip3 uninstall pygame

Python 物件導向 --- 模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.