日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

soap接口怎么不返回tuple python_Python 中的接口

發布時間:2023/12/20 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 soap接口怎么不返回tuple python_Python 中的接口 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python 是動態類型語言, 只在運行時做 Duck Typing 檢查.

  • 利: 靈活, 方便
  • 弊: 代碼混亂, 缺少規范

標準自帶兩類接口支持: abc 和 typing.Protocol, 有他們協助給天馬行空的程序員套上枷鎖, Python 的大工程才可以"上道"

abc

abc 就是 Abstract Base Class, 虛基類. 跟 Java, C++ 中的虛基類是一個意思, 可以對派生類提供實例化時的動態檢查, 確保虛擬接口 (abstractmethod) 都有實現

import abcclass Base(abc.ABC): @abstractmethod def foo(self, s: str): """abc interface demo """class Invalid(Base): pass class Child(Base): def foo(self): pass c = Child()assert isinstance(c, Base)# TypeError: Can't instantiate abstract class Invalid with abstract methods fooi = Invalid()

也提供了非侵入式的虛基類關聯方法

from abc import ABCclass MyABC(ABC): passMyABC.register(tuple)assert issubclass(tuple, MyABC)assert isinstance((), MyABC)
  • 檢查時機: 在運行中當派生類實例化時
  • 檢查范圍: 只確保 abstractmethod 是否在派生類中有相同函數名實現, 并不檢查實現的參數和返回值是否相同. 只看名字不比簽名
  • 代碼影響: 侵入式, 需要繼承. 也有手工非侵入式方案

typing.Protocol

structure subtyping (static duck-typing)

import typingclass Countable(typing.Protocol): def count(self, who: str) -> int: """support count """ class Counter: def count(self, who: str) -> int: return 0 c = Counter()def f(c: Countable): c.count("bill")
  • 檢查時機: 靜態類型檢查接口使用方, 例如 mypy
  • 檢查范圍: 確保實現類按照簽名實現了接口的全部函數
  • 代碼影響: 非侵入式, 不需要繼承

比較

abc 類似 c++ 中的虛基類, typing.Protocol 則好比 c++ 中的 concept.

當然, Python 是動態語言, 在 typing.runtime_checkable 和 abc.abstractmethod 加成后, typing.Protocol 動靜兩相宜

import typing@typing.runtime_checkableclass Countable(typing.Protocol): @abc.abstractmethod def count(self, who: str) -> int: """support count """ class Counter: def count(self, who: str) -> int: return 0 assert issubclass(Counter, Countable)c = Counter()assert isinstance(c, Countable)def f(c: Countable): assert isinstance(c, Countable) print(c.count("bill")) f(c)class InvalidCounter(Countable): def c(self): pass # TypeError: Can't instantiate abstract class InvalidCounter with abstract methods counti = InvalidCounter()

上面這個終極解決方案兼有兩者的優點:

  • 靜態類型檢查時會確保是否在派生類中有相同簽名的實現
  • 動態運行時, 會檢查是否同名函數存在
  • 代碼影響: 自動非侵入式, 不需要繼承, 也無需手工注冊

總結

以上是生活随笔為你收集整理的soap接口怎么不返回tuple python_Python 中的接口的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。