“Talk is cheap. Show me the code.”
― Linus Torvalds
老子第41章
上德若谷
大白若辱
大方無隅
大器晚成
大音希聲
大象無形
道隱無名
拳打千遍, 身法自然
“There’s no shortage of remarkable ideas, what’s missing is the will to execute them.” – Seth Godin
「很棒的點子永遠不會匱乏,然而缺少的是執行點子的意志力。」—賽斯.高汀
高中彈性課程系列
本系列文章之連結
-
從turtle海龜動畫學習Python-高中彈性課程1 link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 2 安裝 Python, 線上執行 Python link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 3 烏龜繪圖 所需之Python基礎 link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 4 烏龜開始畫圖 link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 5 用函數封裝重複性指令-呼叫函數令烏龜畫正 n 邊形 link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 6 畫多重旋轉圓,螺旋正方形 link
-
從turtle海龜動畫 學習 Python - 7 遞歸 recursive - 高中彈性課程系列 link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 8 碎形 (分形 fractal) link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 8.1 碎形 L-system link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 9 Python 物件導向介紹 link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 9.1 Python 物件導向的練習 link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 10 藝術畫 自定義海龜形狀 link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 10.1 藝術畫 python繪製天然雪花結晶 https://blog.csdn.net/m0_47985483/article/details/122262036 link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 10.2 藝術畫 Python 製作生成式藝術 link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 11.1 氣泡排序 - 用 turtle 呈現演算法之執行動作 link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 11.2 maze 迷宮 - 用 turtle 呈現演算法之執行動作 link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 11.3 連分數演算法與轉轉相除法- 用 turtle 呈現演算法之執行動作 link
-
從turtle海龜動畫 學習 Python - 高中彈性課程系列 11.4 最短路徑 Dijkstra- 用 turtle 呈現演算法之執行動作 link
文章目录
前言
原本預計我們要照著 AI Sweigart, Beyond the basic stuff with Pyhton, 這本, 把 Python 物件導向練習一遍.
對於小型程式,
現在決定自己寫一些較簡單切中重點的例子, 用數學的幾何形狀的面積邊長等(屬性)公式之間的包含繼承之關係來詮釋, 我認為是較適合的, 也會讓閱讀者負擔較輕.
像前一篇舉洪錦魁書中的士林銀行跟台北銀行的例子, 就覺得閱讀理解較耗時.
其實物件導向並不能表現物件導向的好處, 反而增加閱讀與維護的負擔.
== AI Sweigart, Beyond the basic stuff with Pyhton
這本書 ch15, 提到, Jack Diederich, 指出, 許多程式員喜歡直接寫物件導向 class, 其實用簡單的函數或模組(module)就能更好的處理完所有工作==.
我經常需要上網找往網友提供的程式碼, 例如直接用 NumPy 打造的 機器學習的 codes, 等, 另外像矩陣等數值計算的程式碼, C 跟 Matlab 很容易找到, 但是 Python 就要花較多時間找, 當很高興的找到, 打開一看, 發現, 老是有人要用物件導向的方式寫任何程式.
數學上數值計算其實是很小的程式(相比於IT界的codes), 一旦用物件導向的方式寫, 我就必須先搞清楚他的類的結構, 裡面的方法跟屬性的定義, 再來看函數的執行, 常感慨, 為何他不直接定義為一個函數就好, 一個小小的高斯消去法, 不是幾行就搞定了嗎?
這個經驗也在學 平行計算 C 的 CUDA 跟 OpenCL時, 有過類似的感覺,
簡單的矩陣乘法的範例, 非要用物件導向來寫, 尤其 AMD 早期在推 OpenCL, 都喜歡用 C++ 物件導向來寫簡單的矩陣計算相關的例子, 讓我覺得 , 是不是很希望別人盡量不要學會 OpenCL 阿, 怕更多人搶自己的飯碗嗎?
我估計, 在 IT 界, 似乎不秀一下物件導向的寫法, 就會覺得自己是菜鳥.
我們常常忘了, 我們學寫程式, 是為了解決問題, 而不是為了跟別人比來比去!
物件導向程式設計: 類別的產生
我們先訂定義 三角形, 正方形, 長方形, 梯形, 多邊形等之類, 它們彼此之間有互相包含或重疊之關係, 正適合用來詮釋物件導向之好處.
我們照前一篇講解的 起手式:
class
三角形:
# 定義初始化函數
縮排 def __init__(self, argument1, argument2,,,):
縮排 初始化函數(方法)的內容,
# 定義面積函數
縮排 def
面積function():
縮排 函數(方法)的內容,
class triangle:
def __init__(self, argument1, argument2,,,):
初始化函數(方法)的內容,,,,,
,,,,,
def area():
函數(方法)的內容
以下為具體之 codes
分別定義 三角形, 長方形, 兩個類
class triangle:
def __init__(self, base, altitude):
self.base = base
self.altitude = altitude
def area(self):
return 1/2*self.base*self.altitude
class rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length*self.width
類的定義執行完之後,
要產生物件, 就使用
>>>triangle(base, altitude)
例如產生一個 底長為 2, 高為 3 的三角形, 取名為 a, 就如下
>>>a = triangle(2, 3)
詳細測試結果
>>> a= triangle(2,3)
>>> a.base
2
>>> a.altitude
3
>>> a.area()
3.0
我們可以再定義一個正方形 square 的類, 並特別針對定義 正方形的面積是邊長平方, 去定義一個正方形的面積函數,
此時正方形類與長方形類沒有關係!
class square:
def __init__(self, length):
self.length = length
def area(self):
return self.length**2
執行與測試結果
>>>
================ RESTART: C:\Users\user\Desktop\geometry_OOP.py ================
>>> c = square(2)
>>> c.length
2
>>> c.width
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
c.width
AttributeError: 'square' object has no attribute 'width'
>>> c.area()
4
使用正方形繼承長方形的方式
也可以把正方形 視為長方形的特例, 直接使用繼承的方式, 直接利用 長方形的面積函數
注意, 繼承的語法, 是在定義子類時用小括號包住父類的名字:
class
square(rectangle):
有了繼承關係, 就可以直接呼叫父類的初始化函數,
rectangle.__init__
(self, length, length)
也可以直接呼叫父類的任何函數來使用
return rectangle.area(self)
完整 codes 如下
class square(rectangle):
def __init__(self, length):
rectangle.__init__(self, length, length)
#self.length = length
def area(self):
return rectangle.area(self)
執行與測試結果
>>>
================ RESTART: C:/Users/user/Desktop/geometry_OOP.py ================
>>> c = square(2)
>>> c.width
2
>>> c.length
2
>>> c.area()
4
物件導向程式設計: 封裝
物件導向程式設計: 多態
Pythonic 風格的 OOP-property 與 dunder 方法 ch17
Reference
- 從turtle海龜動畫 學習 Python - 高中彈性課程系列 9 Python 物件導向介紹
https://blog.csdn.net/m0_47985483/article/details/110688222 link - 對初學者, Python 物件導向, 寫得較深入又不致太繁瑣是以下這本:
AI Sweigart, Beyond the basic stuff with Pyhton,
中譯本: H&C譯, Python 功力提升的樂趣, 基峰出版, 2021.