본문 바로가기

Python

[Python]ABC(Abstract Base Class)

Abstract Base Class

  • ABCmeta class로 선언하면 단독으로 객체 선언이 안되고 상속만 가능한 클래스가 됨.
  • @abstractmethod로 함수에 데코레이터를 선언하면 자식클래스는 필수로 해당 함수를 선언해야함.

 

코드예시

from abc import ABCMeta, abstractmethod


# 해당 클래스는 객체 선언이 안됨.
class ABCMetaTestClass(metaclass=ABCMeta):
    def __init__(self):
        print("Parent Initial")

    @abstractmethod
    def test(self):
        print("Parent Test")


class TestClass(ABCMetaTestClass):
    def __init__(self):
        print("Child Initial")

	# 해당 함수를 선언하지 않으면 에러
    def test(self):
        print("Child Test")
        
        
tmp = TestClass()

 

'Python' 카테고리의 다른 글

[Python] 메모리 관리  (0) 2021.03.18
[Python] GIL(Global Interpreter Lock)  (0) 2021.03.16
[Python] django vs flask  (0) 2021.03.11
[Python] @staticmethod vs @classmethod  (0) 2021.03.04
[Python] is vs ==  (0) 2021.03.01