
- Python 基礎
- Python - 首頁
- Python - 概述
- Python - 歷史
- Python - 特性
- Python vs C++
- Python - Hello World 程式
- Python - 應用領域
- Python - 直譯器
- Python - 環境搭建
- Python - 虛擬環境
- Python - 基本語法
- Python - 變數
- Python - 資料型別
- Python - 型別轉換
- Python - Unicode 系統
- Python - 字面量
- Python - 運算子
- Python - 算術運算子
- Python - 比較運算子
- Python - 賦值運算子
- Python - 邏輯運算子
- Python - 位運算子
- Python - 成員運算子
- Python - 身份運算子
- Python - 運算子優先順序
- Python - 註釋
- Python - 使用者輸入
- Python - 數字
- Python - 布林值
- Python 控制語句
- Python - 控制流
- Python - 決策
- Python - if 語句
- Python - if else
- Python - 巢狀 if
- Python - Match-Case 語句
- Python - 迴圈
- Python - for 迴圈
- Python - for-else 迴圈
- Python - while 迴圈
- Python - break 語句
- Python - continue 語句
- Python - pass 語句
- Python - 巢狀迴圈
- Python 函式與模組
- Python - 函式
- Python - 預設引數
- Python - 關鍵字引數
- Python - 僅關鍵字引數
- Python - 位置引數
- Python - 僅位置引數
- Python - 可變引數
- Python - 變數作用域
- Python - 函式註解
- Python - 模組
- Python - 內建函式
- Python 字串
- Python - 字串
- Python - 字串切片
- Python - 修改字串
- Python - 字串連線
- Python - 字串格式化
- Python - 跳脫字元
- Python - 字串方法
- Python - 字串練習
- Python 列表
- Python - 列表
- Python - 訪問列表元素
- Python - 修改列表元素
- Python - 新增列表元素
- Python - 刪除列表元素
- Python - 遍歷列表
- Python - 列表推導式
- Python - 排序列表
- Python - 複製列表
- Python - 合併列表
- Python - 列表方法
- Python - 列表練習
- Python 元組
- Python - 元組
- Python - 訪問元組元素
- Python - 更新元組
- Python - 解包元組
- Python - 遍歷元組
- Python - 合併元組
- Python - 元組方法
- Python - 元組練習
- Python 集合
- Python - 集合
- Python - 訪問集合元素
- Python - 新增集合元素
- Python - 刪除集合元素
- Python - 遍歷集合
- Python - 合併集合
- Python - 複製集合
- Python - 集合運算子
- Python - 集合方法
- Python - 集合練習
- Python 字典
- Python - 字典
- Python - 訪問字典元素
- Python - 修改字典元素
- Python - 新增字典元素
- Python - 刪除字典元素
- Python - 字典檢視物件
- Python - 遍歷字典
- Python - 複製字典
- Python - 巢狀字典
- Python - 字典方法
- Python - 字典練習
- Python 陣列
- Python - 陣列
- Python - 訪問陣列元素
- Python - 新增陣列元素
- Python - 刪除陣列元素
- Python - 遍歷陣列
- Python - 複製陣列
- Python - 反轉陣列
- Python - 排序陣列
- Python - 合併陣列
- Python - 陣列方法
- Python - 陣列練習
- Python 檔案處理
- Python - 檔案處理
- Python - 寫入檔案
- Python - 讀取檔案
- Python - 重新命名和刪除檔案
- Python - 目錄
- Python - 檔案方法
- Python - OS 檔案/目錄方法
- Python - OS 路徑方法
- 面向物件程式設計
- Python - OOPs 概念
- Python - 類與物件
- Python - 類屬性
- Python - 類方法
- Python - 靜態方法
- Python - 建構函式
- Python - 訪問修飾符
- Python - 繼承
- Python - 多型
- Python - 方法重寫
- Python - 方法過載
- Python - 動態繫結
- Python - 動態型別
- Python - 抽象
- Python - 封裝
- Python - 介面
- Python - 包
- Python - 內部類
- Python - 匿名類和物件
- Python - 單例類
- Python - 包裝類
- Python - 列舉
- Python - 反射
- Python 錯誤與異常
- Python - 語法錯誤
- Python - 異常
- Python - try-except 塊
- Python - try-finally 塊
- Python - 丟擲異常
- Python - 異常鏈
- Python - 巢狀 try 塊
- Python - 使用者自定義異常
- Python - 日誌記錄
- Python - 斷言
- Python - 內建異常
- Python 多執行緒
- Python - 多執行緒
- Python - 執行緒生命週期
- Python - 建立執行緒
- Python - 啟動執行緒
- Python - 加入執行緒
- Python - 執行緒命名
- Python - 執行緒排程
- Python - 執行緒池
- Python - 主執行緒
- Python - 執行緒優先順序
- Python - 守護執行緒
- Python - 執行緒同步
- Python 同步
- Python - 執行緒間通訊
- Python - 執行緒死鎖
- Python - 中斷執行緒
- Python 網路程式設計
- Python - 網路程式設計
- Python - 套接字程式設計
- Python - URL 處理
- Python - 泛型
- Python 庫
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 雜項
- Python - 日期與時間
- Python - 數學
- Python - 迭代器
- Python - 生成器
- Python - 閉包
- Python - 裝飾器
- Python - 遞迴
- Python - 正則表示式
- Python - PIP
- Python - 資料庫訪問
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 輸出格式化
- Python - 效能測量
- Python - 資料壓縮
- Python - CGI 程式設計
- Python - XML 處理
- Python - GUI 程式設計
- Python - 命令列引數
- Python - 文件字串
- Python - JSON
- Python - 傳送郵件
- Python - 擴充套件
- Python - 工具/實用程式
- Python - GUIs
- Python 高階概念
- Python - 抽象基類
- Python - 自定義異常
- Python - 高階函式
- Python - 物件內部結構
- Python - 記憶體管理
- Python - 元類
- Python - 使用元類進行超程式設計
- Python - 模擬和存根
- Python - Monkey Patching
- Python - 訊號處理
- Python - 型別提示
- Python - 自動化教程
- Python - Humanize 包
- Python - 上下文管理器
- Python - 協程
- Python - 描述符
- Python - 診斷和修復記憶體洩漏
- Python - 不可變資料結構
- Python 有用資源
- Python - 問答
- Python - 線上測驗
- Python - 快速指南
- Python - 參考
- Python - 速查表
- Python - 專案
- Python - 有用資源
- Python - 討論
- Python 編譯器
- NumPy 編譯器
- Matplotlib 編譯器
- SciPy 編譯器
Python - 類和物件
Python 是一種面向物件程式語言,這意味著它基於 OOP 概念的原則。Python 程式中使用的實體是某個類的物件。例如,數字、字串、列表、字典以及程式中類似的其他實體都是相應內建類的物件。
在 Python 中,名為Object的類是所有類的基類或父類,包括內建類和使用者定義類。
什麼是 Python 中的類?
在 Python 中,class 是使用者定義的實體(資料型別),它定義了物件可以包含的資料型別以及可以執行的操作。它用作建立物件的模板。例如,如果我們想在 Python 程式中定義一個智慧手機類,我們可以使用 RAM、ROM、螢幕尺寸等資料型別以及撥打電話和傳送簡訊等操作。
在 Python 中建立類
使用class 關鍵字在 Python 中建立一個新類。class關鍵字後緊跟類名,然後是一個冒號,如下所示:
class ClassName: 'Optional class documentation string' class_suite
該類具有一個文件字串,可以透過 ClassName.__doc__ 訪問。
class_suite 包含所有定義類成員、資料屬性和函式的元件語句。
示例
以下是一個簡單的 Python 類示例:
class Employee: 'Common base class for all employees' empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print "Total Employee %d" % Employee.empCount def displayEmployee(self): print "Name : ", self.name, ", Salary: ", self.salary
變數empCount 是一個類變數,其值在該類的所有例項之間共享。可以從類內或類外作為Employee.empCount訪問。
第一個方法__init__()是一個特殊方法,稱為類建構函式或初始化方法,當建立此類的新的例項時,Python 會呼叫它。
您可以像宣告普通函式一樣宣告其他類方法,區別在於每個方法的第一個引數是self。Python 會為您新增self 引數;呼叫方法時,不需要包含它。
什麼是物件?
一個物件被稱為給定Python類的例項。每個物件都有自己的屬性和方法,這些屬性和方法由其類定義。
建立類時,它只描述物件的結構。當從類例項化物件時,才會分配記憶體。

在上圖中,Vehicle是類名,Car、Bus和SUV是它的物件。
在Python中建立類的物件
要建立類的例項,可以使用類名呼叫類,並傳入其__init__方法接受的任何引數。
# This would create first object of Employee class emp1 = Employee("Zara", 2000) # This would create second object of Employee class emp2 = Employee("Manni", 5000)
在Python中訪問物件的屬性
可以使用點運算子 object. 來訪問物件的屬性。類變數的訪問方式如下:
emp1.displayEmployee() emp2.displayEmployee() print ("Total Employee %d" % Employee.empCount)
現在,將所有概念放在一起:
class Employee: "Common base class for all employees" empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print ("Total Employee %d" % Employee.empCount) def displayEmployee(self): print ("Name : ", self.name, ", Salary: ", self.salary) # This would create first object of Employee class emp1 = Employee("Zara", 2000) # This would create second object of Employee class emp2 = Employee("Manni", 5000) emp1.displayEmployee() emp2.displayEmployee() print ("Total Employee %d" % Employee.empCount)
執行上述程式碼後,將產生以下結果:
Name : Zara , Salary: 2000 Name : Manni , Salary: 5000 Total Employee 2
可以隨時新增、刪除或修改類和物件的屬性:
# Add an 'age' attribute emp1.age = 7 # Modify 'age' attribute emp1.age = 8 # Delete 'age' attribute del emp1.age
除了使用正常的語句訪問屬性外,還可以使用以下函式:
getattr(obj, name[, default]) − 訪問物件的屬性。
hasattr(obj,name) − 檢查屬性是否存在。
setattr(obj,name,value) − 設定屬性。如果屬性不存在,則會建立它。
delattr(obj, name) − 刪除屬性。
# Returns true if 'age' attribute exists hasattr(emp1, 'age') # Returns value of 'age' attribute getattr(emp1, 'age') # Set attribute 'age' at 8 setattr(emp1, 'age', 8) # Delete attribute 'age' delattr(empl, 'age')
Python中的內建類屬性
每個Python類都保留以下內建屬性,可以使用點運算子像任何其他屬性一樣訪問它們:
序號 | 屬性和描述 |
---|---|
1 | __dict__
包含類名稱空間的字典。 |
2 | __doc__
類文件字串,如果未定義則為None。 |
3 | __name__
類名 |
4 | __module__
定義類的模組名。在互動模式下,此屬性為“__main__”。 |
5 | __bases__
一個可能為空的元組,包含基類,其順序與其在基類列表中的出現順序相同。 |
示例
對於上面的Employee類,讓我們嘗試訪問它的屬性:
class Employee: 'Common base class for all employees' empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print ("Total Employee %d" % Employee.empCount) def displayEmployee(self): print ("Name : ", self.name, ", Salary: ", self.salary) print ("Employee.__doc__:", Employee.__doc__) print ("Employee.__name__:", Employee.__name__) print ("Employee.__module__:", Employee.__module__) print ("Employee.__bases__:", Employee.__bases__) print ("Employee.__dict__:", Employee.__dict__)
執行上述程式碼後,將產生以下結果:
Employee.__doc__: Common base class for all employees Employee.__name__: Employee Employee.__module__: __main__ Employee.__bases__: () Employee.__dict__: {'__module__': '__main__', 'displayCount': <function displayCount at 0xb7c84994>, 'empCount': 2, 'displayEmployee': <function displayEmployee at 0xb7c8441c>, '__doc__': 'Common base class for all employees', '__init__': <function __init__ at 0xb7c846bc>}
Python 資料型別的內建類
如前所述,Python遵循面向物件的程式設計正規化。字串、列表和資料型別等實體屬於一個或多個內建類。
如果我們想檢視哪種資料型別屬於哪個內建類,可以使用Python的type()函式。此函式接受資料型別並返回其對應的類。
示例
以下示例演示如何檢查給定資料型別的內建類。
num = 20 print (type(num)) num1 = 55.50 print (type(num1)) s = "TutorialsPoint" print (type(s)) dct = {'a':1,'b':2,'c':3} print (type(dct)) def SayHello(): print ("Hello World") return print (type(SayHello))
執行此程式碼時,它將顯示Python資料型別的對應類:
<class 'int'> <class 'float'> <class 'str'> <class 'dict'> <class 'function'>
Python中的垃圾回收(銷燬物件)
Python會自動刪除不需要的物件(內建型別或類例項)以釋放記憶體空間。Python定期回收不再使用的記憶體塊的過程稱為垃圾回收。
Python的垃圾收集器在程式執行期間執行,並在物件的引用計數達到零時觸發。物件的引用計數會隨著指向它的別名數量的變化而變化。
當物件被賦予新名稱或放入容器(列表、元組或字典)中時,物件的引用計數會增加。當物件使用del刪除、其引用被重新分配或其引用超出範圍時,物件的引用計數會減少。當物件的引用計數達到零時,Python會自動收集它。
# Create object <40> a = 40 # Increase ref. count of <40> b = a # Increase ref. count of <40> c = [b] # Decrease ref. count of <40> del a # Decrease ref. count of <40> b = 100 # Decrease ref. count of <40> c[0] = -1
通常情況下,您不會注意到垃圾收集器何時銷燬未使用的例項並回收其空間。但是,類可以實現名為解構函式的特殊方法__del__(),該方法在例項即將被銷燬時呼叫。此方法可用於清理例項使用的任何非記憶體資源。
示例
__del__()解構函式列印即將被銷燬的例項的類名,如下面的程式碼塊所示:
class Point: def __init__( self, x=0, y=0): self.x = x self.y = y def __del__(self): class_name = self.__class__.__name__ print (class_name, "destroyed") pt1 = Point() pt2 = pt1 pt3 = pt1 # prints the ids of the obejcts print (id(pt1), id(pt2), id(pt3)) del pt1 del pt2 del pt3
執行後,上述程式碼將產生以下結果:
135007479444176 135007479444176 135007479444176 Point destroyed
Python中的資料隱藏
物件的屬性可能在類定義之外可見,也可能不可見。需要用雙下劃線字首命名屬性,這些屬性將無法直接被外部訪問。
示例
class JustCounter: __secretCount = 0 def count(self): self.__secretCount += 1 print self.__secretCount counter = JustCounter() counter.count() counter.count() print counter.__secretCount
執行上述程式碼後,將產生以下結果:
1 2 ERROR! Traceback (most recent call last): File <main.py>", line 11, in <module> AttributeError: 'JustCounter' object has no attribute '__secretCount'
Python 透過在內部更改名稱以包含類名來保護這些成員。您可以訪問這些屬性,例如 object._className__attrName。如果將最後一行替換為以下內容,則它對您有效:
print(counter._JustCounter__secretCount)
執行上述程式碼後,將產生以下結果:
1 2 2