<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ### 導航 - [索引](../genindex.xhtml "總目錄") - [模塊](../py-modindex.xhtml "Python 模塊索引") | - [下一頁](atexit.xhtml "atexit --- 退出處理器") | - [上一頁](contextlib.xhtml "contextlib --- Utilities for with-statement contexts") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) ? - zh\_CN 3.7.3 [文檔](../index.xhtml) ? - [Python 標準庫](index.xhtml) ? - [Python運行時服務](python.xhtml) ? - $('.inline-search').show(0); | # [`abc`](#module-abc "abc: Abstract base classes according to PEP 3119.") --- 抽象基類 **源代碼:** [Lib/abc.py](https://github.com/python/cpython/tree/3.7/Lib/abc.py) \[https://github.com/python/cpython/tree/3.7/Lib/abc.py\] - - - - - - 該模塊提供了在 Python 中定義 [抽象基類](../glossary.xhtml#term-abstract-base-class) (ABC) 的組件,在 [**PEP 3119**](https://www.python.org/dev/peps/pep-3119) \[https://www.python.org/dev/peps/pep-3119\] 中已有概述。查看 PEP 文檔了解為什么需要在 Python 中增加這個模塊。(也可查看 [**PEP 3141**](https://www.python.org/dev/peps/pep-3141) \[https://www.python.org/dev/peps/pep-3141\] 以及 [`numbers`](numbers.xhtml#module-numbers "numbers: Numeric abstract base classes (Complex, Real, Integral, etc.).") 模塊了解基于 ABC 的數字類型繼承關系。) [`collections`](collections.xhtml#module-collections "collections: Container datatypes") 模塊中有一些派生自 ABC 的具體類;當然這些類還可以進一步被派生。此外,[`collections.abc`](collections.abc.xhtml#module-collections.abc "collections.abc: Abstract base classes for containers") 子模塊中有一些 ABC 可被用于測試一個類或實例是否提供特定的接口,例如它是否可哈希或它是否為映射等。 該模塊提供了一個元類 [`ABCMeta`](#abc.ABCMeta "abc.ABCMeta"),可以用來定義抽象類,另外還提供一個工具類 [`ABC`](#abc.ABC "abc.ABC"),可以用它以繼承的方式定義抽象基類。 *class* `abc.``ABC`一個使用 [`ABCMeta`](#abc.ABCMeta "abc.ABCMeta") 作為元類的工具類。抽象基類可以通過從 [`ABC`](#abc.ABC "abc.ABC") 派生來簡單地創建,這就避免了在某些情況下會令人混淆的元類用法,例如: ``` from abc import ABC class MyABC(ABC): pass ``` 注意 [`ABC`](#abc.ABC "abc.ABC") 的類型仍然是 [`ABCMeta`](#abc.ABCMeta "abc.ABCMeta"),因此繼承 [`ABC`](#abc.ABC "abc.ABC") 仍然需要關注元類使用中的注意事項,比如可能會導致元類沖突的多重繼承。當然你也可以直接使用 [`ABCMeta`](#abc.ABCMeta "abc.ABCMeta") 作為元類來定義抽象基類,例如: ``` from abc import ABCMeta class MyABC(metaclass=ABCMeta): pass ``` 3\.4 新版功能. *class* `abc.``ABCMeta`用于定義抽象基類(ABC)的元類。 使用該元類以創建抽象基類。抽象基類可以像 mix-in 類一樣直接被子類繼承。你也可以將不相關的具體類(包括內建類)和抽象基類注冊為“抽象子類” —— 這些類以及它們的子類會被內建函數 [`issubclass()`](functions.xhtml#issubclass "issubclass") 識別為對應的抽象基類的子類,但是該抽象基類不會出現在其 MRO(Method Resolution Order,方法解析順序)中,抽象基類中實現的方法也不可調用(即使通過 [`super()`](functions.xhtml#super "super") 調用也不行)。[1](#id2) 使用 [`ABCMeta`](#abc.ABCMeta "abc.ABCMeta") 作為元類創建的類含有如下方法: `register`(*subclass*)將“子類”注冊為該抽象基類的“抽象子類”,例如: ``` from abc import ABC class MyABC(ABC): pass MyABC.register(tuple) assert issubclass(tuple, MyABC) assert isinstance((), MyABC) ``` 在 3.3 版更改: 返回注冊的子類,使其能夠作為類裝飾器。 在 3.4 版更改: 你可以使用 [`get_cache_token()`](#abc.get_cache_token "abc.get_cache_token") 函數來檢測對 [`register()`](#abc.ABCMeta.register "abc.ABCMeta.register") 的調用。 你也可以在虛基類中重載這個方法。 `__subclasshook__`(*subclass*)(必須定義為類方法。) 檢查 *subclass* 是否是該抽象基類的子類。也就是說對于那些你希望定義為該抽象基類的子類的類,你不用對每個類都調用 [`register()`](#abc.ABCMeta.register "abc.ABCMeta.register") 方法了,而是可以直接自定義 `issubclass` 的行為。(這個類方法是在抽象基類的 `__subclasscheck__()` 方法中調用的。) 該方法必須返回 `True`, `False` 或是 `NotImplemented`。如果返回 `True`,*subclass* 就會被認為是這個抽象基類的子類。如果返回 `False`,無論正常情況是否應該認為是其子類,統一視為不是。如果返回 `NotImplemented`,子類檢查會按照正常機制繼續執行。 為了對這些概念做一演示,請看以下定義 ABC 的示例: ``` class Foo: def __getitem__(self, index): ... def __len__(self): ... def get_iterator(self): return iter(self) class MyIterable(ABC): @abstractmethod def __iter__(self): while False: yield None def get_iterator(self): return self.__iter__() @classmethod def __subclasshook__(cls, C): if cls is MyIterable: if any("__iter__" in B.__dict__ for B in C.__mro__): return True return NotImplemented MyIterable.register(Foo) ``` ABC `MyIterable` 定義了標準的迭代方法 [`__iter__()`](stdtypes.xhtml#iterator.__iter__ "iterator.__iter__") 作為一個抽象方法。這里給出的實現仍可在子類中被調用。`get_iterator()` 方法也是 `MyIterable` 抽象基類的一部分,但它并非必須被非抽象派生類所重載。 The [`__subclasshook__()`](#abc.ABCMeta.__subclasshook__ "abc.ABCMeta.__subclasshook__") class method defined here says that any class that has an [`__iter__()`](stdtypes.xhtml#iterator.__iter__ "iterator.__iter__") method in its [`__dict__`](stdtypes.xhtml#object.__dict__ "object.__dict__") (or in that of one of its base classes, accessed via the [`__mro__`](stdtypes.xhtml#class.__mro__ "class.__mro__") list) is considered a `MyIterable` too. Finally, the last line makes `Foo` a virtual subclass of `MyIterable`, even though it does not define an [`__iter__()`](stdtypes.xhtml#iterator.__iter__ "iterator.__iter__") method (it uses the old-style iterable protocol, defined in terms of [`__len__()`](../reference/datamodel.xhtml#object.__len__ "object.__len__") and [`__getitem__()`](../reference/datamodel.xhtml#object.__getitem__ "object.__getitem__")). Note that this will not make `get_iterator`available as a method of `Foo`, so it is provided separately. 此外,[`abc`](#module-abc "abc: Abstract base classes according to PEP 3119.") 模塊還提供了這些裝飾器: `@``abc.``abstractmethod`用于聲明抽象方法的裝飾器。 使用此裝飾器要求類的元類是 [`ABCMeta`](#abc.ABCMeta "abc.ABCMeta") 或是從該類派生。一個具有派生自 [`ABCMeta`](#abc.ABCMeta "abc.ABCMeta") 的元類的類不可以被實例化,除非它全部的抽象方法和特征屬性均已被重載。抽象方法可通過任何普通的“super”調用機制來調用。 [`abstractmethod()`](#abc.abstractmethod "abc.abstractmethod") 可被用于聲明特性屬性和描述器的抽象方法。 Dynamically adding abstract methods to a class, or attempting to modify the abstraction status of a method or class once it is created, are not supported. The [`abstractmethod()`](#abc.abstractmethod "abc.abstractmethod") only affects subclasses derived using regular inheritance; "virtual subclasses" registered with the ABC's `register()` method are not affected. When [`abstractmethod()`](#abc.abstractmethod "abc.abstractmethod") is applied in combination with other method descriptors, it should be applied as the innermost decorator, as shown in the following usage examples: ``` class C(ABC): @abstractmethod def my_abstract_method(self, ...): ... @classmethod @abstractmethod def my_abstract_classmethod(cls, ...): ... @staticmethod @abstractmethod def my_abstract_staticmethod(...): ... @property @abstractmethod def my_abstract_property(self): ... @my_abstract_property.setter @abstractmethod def my_abstract_property(self, val): ... @abstractmethod def _get_x(self): ... @abstractmethod def _set_x(self, val): ... x = property(_get_x, _set_x) ``` In order to correctly interoperate with the abstract base class machinery, the descriptor must identify itself as abstract using `__isabstractmethod__`. In general, this attribute should be `True`if any of the methods used to compose the descriptor are abstract. For example, Python's built-in [`property`](functions.xhtml#property "property") does the equivalent of: ``` class Descriptor: ... @property def __isabstractmethod__(self): return any(getattr(f, '__isabstractmethod__', False) for f in (self._fget, self._fset, self._fdel)) ``` 注解 Unlike Java abstract methods, these abstract methods may have an implementation. This implementation can be called via the [`super()`](functions.xhtml#super "super") mechanism from the class that overrides it. This could be useful as an end-point for a super-call in a framework that uses cooperative multiple-inheritance. The [`abc`](#module-abc "abc: Abstract base classes according to PEP 3119.") module also supports the following legacy decorators: `@``abc.``abstractclassmethod`3\.2 新版功能. 3\.3 版后已移除: It is now possible to use [`classmethod`](functions.xhtml#classmethod "classmethod") with [`abstractmethod()`](#abc.abstractmethod "abc.abstractmethod"), making this decorator redundant. A subclass of the built-in [`classmethod()`](functions.xhtml#classmethod "classmethod"), indicating an abstract classmethod. Otherwise it is similar to [`abstractmethod()`](#abc.abstractmethod "abc.abstractmethod"). This special case is deprecated, as the [`classmethod()`](functions.xhtml#classmethod "classmethod") decorator is now correctly identified as abstract when applied to an abstract method: ``` class C(ABC): @classmethod @abstractmethod def my_abstract_classmethod(cls, ...): ... ``` `@``abc.``abstractstaticmethod`3\.2 新版功能. 3\.3 版后已移除: It is now possible to use [`staticmethod`](functions.xhtml#staticmethod "staticmethod") with [`abstractmethod()`](#abc.abstractmethod "abc.abstractmethod"), making this decorator redundant. A subclass of the built-in [`staticmethod()`](functions.xhtml#staticmethod "staticmethod"), indicating an abstract staticmethod. Otherwise it is similar to [`abstractmethod()`](#abc.abstractmethod "abc.abstractmethod"). This special case is deprecated, as the [`staticmethod()`](functions.xhtml#staticmethod "staticmethod") decorator is now correctly identified as abstract when applied to an abstract method: ``` class C(ABC): @staticmethod @abstractmethod def my_abstract_staticmethod(...): ... ``` `@``abc.``abstractproperty`3\.3 版后已移除: It is now possible to use [`property`](functions.xhtml#property "property"), `property.getter()`, `property.setter()` and `property.deleter()` with [`abstractmethod()`](#abc.abstractmethod "abc.abstractmethod"), making this decorator redundant. A subclass of the built-in [`property()`](functions.xhtml#property "property"), indicating an abstract property. This special case is deprecated, as the [`property()`](functions.xhtml#property "property") decorator is now correctly identified as abstract when applied to an abstract method: ``` class C(ABC): @property @abstractmethod def my_abstract_property(self): ... ``` The above example defines a read-only property; you can also define a read-write abstract property by appropriately marking one or more of the underlying methods as abstract: ``` class C(ABC): @property def x(self): ... @x.setter @abstractmethod def x(self, val): ... ``` If only some components are abstract, only those components need to be updated to create a concrete property in a subclass: ``` class D(C): @C.x.setter def x(self, val): ... ``` [`abc`](#module-abc "abc: Abstract base classes according to PEP 3119.") 模塊還提供了這些函數: `abc.``get_cache_token`()返回當前抽象基類的緩存令牌 The token is an opaque object (that supports equality testing) identifying the current version of the abstract base class cache for virtual subclasses. The token changes with every call to [`ABCMeta.register()`](#abc.ABCMeta.register "abc.ABCMeta.register") on any ABC. 3\.4 新版功能. 腳注 [1](#id1)C++ 程序員需要注意:Python 中虛基類的概念和 C++ 中的并不相同。 ### 導航 - [索引](../genindex.xhtml "總目錄") - [模塊](../py-modindex.xhtml "Python 模塊索引") | - [下一頁](atexit.xhtml "atexit --- 退出處理器") | - [上一頁](contextlib.xhtml "contextlib --- Utilities for with-statement contexts") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) ? - zh\_CN 3.7.3 [文檔](../index.xhtml) ? - [Python 標準庫](index.xhtml) ? - [Python運行時服務](python.xhtml) ? - $('.inline-search').show(0); | ? [版權所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 軟件基金會是一個非盈利組織。 [請捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [發現了問題](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 創建。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看