Python:abc
This module provides the infrastructure for defining abstract base classes (ABCs) in Python, as outlined in PEP 3119; see the PEP for why this was added to Python. (See also PEP 3141 and the numbers module regarding a type hierarchy for numbers based on ABCs.)
Example
import abc
class BaseClass(metaclass=ABCMeta):
@abc.abstractmethod
def func1(self):
pass
@abc.abstractmethod
def func2(self):
pass
abstractmethod
property abstractmethod
from abc import ABC, abstractmethod
class A(ABC):
@property
@abstractmethod
def mode(self) -> str:
pass
@override로 사용시 데코레이터 순서:
abstractclassmethod
Deprecated. 다음과 같이 대체 사용:
abstractstaticmethod
Deprecated. 다음과 같이 대체 사용:
abstractproperty
Deprecated. 다음과 같이 대체 사용:
ABC vs Protocol
요약하면, 이미 상속 구조가 잡혀있어서 컨트롤 하기 힘들다면 Protocol, 처음부터 설계 가능하다면 ABC
Metaclass
See also
- Python
- Abstract Base Classes