Python中的面向?qū)ο鬀]有意義
近來,許多人都在抨擊面向?qū)ο螅m然我不認(rèn)為面向?qū)ο蟊旧碛惺裁磫栴},但我覺得至少在 Python 中沒這個必要。
1、沒有必要使用面向?qū)ο?/h3>
舉個例子,比如下面的代碼完全沒有必要使用面向?qū)ο蟆?nbsp;
- class ApiClient:
- def __init__(self, root_url: str, session_cls: sessionmaker):
- self.root_url = root_url
- self.session_cls = session_cls
- def construct_url(self, entity: str) -> str:
- returnf"{self.root_url}/v1/{entity}"
- def get_items(self,entity: str) -> List[Item]:
- resp = requests.get(self.construct_url(entity))
- resp.raise_for_status()
- return [Item(**n) for n in resp.json()["items"]]
- def save_items(self, entity: str) -> None:
- with scoped_session(self.session_cls)as session:
- session.add(self.get_items(entity))
- class ClientA(ApiClient):
- def construct_url(self, entity: str) -> str:
- returnf"{self.root_url}/{entity}"
- class ClientB(ApiClient):
- def construct_url(self, entity: str) -> str:
- returnf"{self.root_url}/a/special/place/{entity}"
- client_a = ClientA("https://client-a",session_cls)
- client_a.save_items("bars")
這里使用了面向?qū)ο螅驗槲覀兿氚?root_url 綁定到某個對象上,而且不想每次都傳遞 sessionmaker。我們還想使用繼承,在調(diào)用的中途訪問一個方法。
但如果只通過數(shù)據(jù)傳遞和函數(shù)能實現(xiàn)嗎?
- @dataclass
- class Client:
- root_url: str
- url_layout: str
- client_a = Client(
- root_url="https://client-a",
- url_layout="{root_url}/{entity}",
- )
- client_b = Client(
- root_url="https://client-b",
- url_layout="{root_url}/a/special/place/{entity}",
- )
- def construct_url(client: Client, entity: str) -> str:
- returnclient.url_layout.format(root_url=client.root_url, entityentity=entity)
- def get_items(client: Client, entity: str) -> List[Item]:
- resp = requests.get(construct_url(client, entity))
- resp.raise_for_status()
- return [Item(**n) for n in resp.json()["items"]]
- def save_items(client: Client, session_cls: session_cls, entity: str) -> None:
- withscoped_session(session_cls) as session:
- session.add(get_items(client, entity))
- save_items(client_a,session_cls, "bars")
我們必須隨時傳遞 Client 和 session_cls。
但有什么關(guān)系呢?代碼量甚至還少了 10%。這樣編寫的代碼很容易理解,而且不需要使用面向?qū)ο蟆?/p>
有人管這種寫法叫做“函數(shù)袋”。就是說,整個代碼都由有類型的數(shù)據(jù)和一大堆模塊作用域的函數(shù)組成。
那么全局變量怎么處理?你可以參考這篇文章(https://leontrolski.github.io/sane-config.html),在整個應(yīng)用程序的生命周期內(nèi)重用 config 或 db 的 session,
接口、抽象類怎么辦?實際上你不需要它們,直接寫代碼就行了。平心而論,Python 有了類型標(biāo)注之后,函數(shù)袋風(fēng)格才開始發(fā)揮真正的魅力。
不純粹的函數(shù)怎么辦?
如果你想采用純粹的函數(shù)式編程,你可能想編寫純粹的類,然后使用不純粹的“適配器”實例來做一些處理:getting-the-current-datetime/API-calls/talking-to-the-db/other-impure-stuff。這個想法很不錯。實際上你可以直接使用 freezegun、responses 等方法來避免大量麻煩。
2、例外
但也有一些例外的情況:
- 你可能注意到,重構(gòu)的代碼中加入了@dataclass,它們只是記錄類型。Python 5 可以直接支持這些,不需要使用“常規(guī)”類。
- 使用 Exception 的子類是沒問題的。使用 try: ... except SomeClass: ...,基本上會形成一種層級,不過沒關(guān)系,只要不要搞得過于復(fù)雜。
- Enum,與上面一樣,它們非常適合 Python。
在極罕見的情況下(至少在應(yīng)用程序的開發(fā)中很少遇到),你可能會想出一種非常好用的類型然后到處使用,就像pandas.DataFrame/sqlalchemy.Session 一樣。但是一般情況下,不要自欺欺人,不要騙自己說我們正在構(gòu)建了不起的應(yīng)用程序。謙虛使人進(jìn)步。
3、面向?qū)ο蟮谋锥?/h3>
雖然在本文開頭,我說過我不認(rèn)為面向?qū)ο蟊旧碛惺裁磫栴},但實際上我還是覺得面向?qū)ο蟛粌H沒有幫助性,而且還常?;煜龁栴},鼓勵一些不良做法:
- 面向?qū)ο蠊膭钅阈薷臄?shù)據(jù)。函數(shù)袋非常反對修改參數(shù)。不相信的話,你可以試試看,但可千萬別生氣。
- 面向?qū)ο笾皇欠祷氐娜肿兞俊D銦o法在函數(shù)之間共享數(shù)據(jù),self 會強(qiáng)迫你使用更小的狀態(tài)空間編寫方便測試的函數(shù)。
- 混合數(shù)據(jù)和函數(shù)會加劇序列化的難度,而在當(dāng)今 REST API 流行的情況下,序列化非常有用。
- 面向?qū)ο髱砹睡偪竦睦^承體系,關(guān)于這個話題的討論到處都是。
- 最重要的是,面向?qū)ο鬀]有任何附加價值,它只會導(dǎo)致你無法專心解決問題,并加劇瀏覽與理解代碼的難度。