<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Python 中的面向?qū)ο鬀](méi)有意義

          共 3230字,需瀏覽 7分鐘

           ·

          2021-04-07 11:28


          許多人都在抨擊面向?qū)ο螅m然我不認(rèn)為他有什么問(wèn)題,但我覺(jué)得至少 Python 中沒(méi)必要。

          近來(lái),許多人都在抨擊面向?qū)ο螅m然我不認(rèn)為面向?qū)ο蟊旧碛惺裁磫?wèn)題,但我覺(jué)得至少在 Python 中沒(méi)這個(gè)必要。


          1、沒(méi)有必要使用面向?qū)ο?/span>

          舉個(gè)例子,比如下面的代碼完全沒(méi)有必要使用面向?qū)ο蟆?/span>
          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ū)ο螅驗(yàn)槲覀兿氚?root_url 綁定到某個(gè)對(duì)象上,而且不想每次都傳遞 sessionmaker。我們還想使用繼承,在調(diào)用的中途訪問(wèn)一個(gè)方法。
          但如果只通過(guò)數(shù)據(jù)傳遞和函數(shù)能實(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, entity=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")

          我們必須隨時(shí)傳遞 Client 和 session_cls。
          但有什么關(guān)系呢?代碼量甚至還少了 10%。這樣編寫(xiě)的代碼很容易理解,而且不需要使用面向?qū)ο蟆?/span>
          有人管這種寫(xiě)法叫做“函數(shù)袋”。就是說(shuō),整個(gè)代碼都由有類(lèi)型的數(shù)據(jù)和一大堆模塊作用域的函數(shù)組成。
          那么全局變量怎么處理?你可以參考這篇文章(https://leontrolski.github.io/sane-config.html),在整個(gè)應(yīng)用程序的生命周期內(nèi)重用 config 或 db 的 session,
          接口、抽象類(lèi)怎么辦?實(shí)際上你不需要它們,直接寫(xiě)代碼就行了。平心而論,Python 有了類(lèi)型標(biāo)注之后,函數(shù)袋風(fēng)格才開(kāi)始發(fā)揮真正的魅力。
          不純粹的函數(shù)怎么辦?
          如果你想采用純粹的函數(shù)式編程,你可能想編寫(xiě)純粹的類(lèi),然后使用不純粹的“適配器”實(shí)例來(lái)做一些處理:getting-the-current-datetime/API-calls/talking-to-the-db/other-impure-stuff。這個(gè)想法很不錯(cuò)。實(shí)際上你可以直接使用 freezegun、responses 等方法來(lái)避免大量麻煩。


          2、例外

          但也有一些例外的情況:
          • 你可能注意到,重構(gòu)的代碼中加入了@dataclass,它們只是記錄類(lèi)型。Python 5 可以直接支持這些,不需要使用“常規(guī)”類(lèi)。
          • 使用 Exception 的子類(lèi)是沒(méi)問(wèn)題的。使用 try: ... except SomeClass: ...,基本上會(huì)形成一種層級(jí),不過(guò)沒(méi)關(guān)系,只要不要搞得過(guò)于復(fù)雜。
          • Enum,與上面一樣,它們非常適合 Python。
          • 在極罕見(jiàn)的情況下(至少在應(yīng)用程序的開(kāi)發(fā)中很少遇到),你可能會(huì)想出一種非常好用的類(lèi)型然后到處使用,就像pandas.DataFrame/sqlalchemy.Session 一樣。但是一般情況下,不要自欺欺人,不要騙自己說(shuō)我們正在構(gòu)建了不起的應(yīng)用程序。謙虛使人進(jìn)步。


          3、面向?qū)ο蟮谋锥?/span>

          雖然在本文開(kāi)頭,我說(shuō)過(guò)我不認(rèn)為面向?qū)ο蟊旧碛惺裁磫?wèn)題,但實(shí)際上我還是覺(jué)得面向?qū)ο蟛粌H沒(méi)有幫助性,而且還常常混淆問(wèn)題,鼓勵(lì)一些不良做法:
          • 面向?qū)ο蠊膭?lì)你修改數(shù)據(jù)。函數(shù)袋非常反對(duì)修改參數(shù)。不相信的話,你可以試試看,但可千萬(wàn)別生氣。
          • 面向?qū)ο笾皇欠祷氐娜肿兞俊D銦o(wú)法在函數(shù)之間共享數(shù)據(jù),self 會(huì)強(qiáng)迫你使用更小的狀態(tài)空間編寫(xiě)方便測(cè)試的函數(shù)。
          • 混合數(shù)據(jù)和函數(shù)會(huì)加劇序列化的難度,而在當(dāng)今 REST API 流行的情況下,序列化非常有用。
          • 面向?qū)ο髱?lái)了瘋狂的繼承體系,關(guān)于這個(gè)話題的討論到處都是。
          • 最重要的是,面向?qū)ο鬀](méi)有任何附加價(jià)值,它只會(huì)導(dǎo)致你無(wú)法專心解決問(wèn)題,并加劇瀏覽與理解代碼的難度。
          參考鏈接:https://leontrolski.github.io/mostly-pointless.html
          文章轉(zhuǎn)自:Python程序員(版權(quán)歸原作者所有,侵刪)


          瀏覽 52
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产777无码 | 天天插天天日 | 国产精品天堂 | 天天色天天干天天色 | 无码视频在线免费观看 |