[Python] abstract class

UNGGI LEE·2023년 9월 4일
0

익숙하지 않은 문법이라 정리를 위해 기록합니다.

Abstract class란?

파이썬에서 abstract class는 하위 method의 목록만 가진 class이다. 이 class는 상속받는 class에서 method 구현을 강제하기 위해 사용된다.

사용방법

아래처럼 ABCMeta를 import해서 원하는 abstract_class의 metaclass의 인자값으로 넣어주고, 원하는 하위 method에 @abstractmethod 라는 데코레이터를 붙여주면 된다.

from abc import ABCMeta

class abstract_class(metaclass=ABCMeta):
	@abstractmethod
    def method1(self):
    	pass

그러면 이 class를 상속받는 class는 반드시 method1을 작성해야한다. 그렇지 않으면 error를 발생시킨다.

Reference

https://dojang.io/mod/page/view.php?id=2389

profile
NLP Researcher & ML Engineer @i-Scream Edu

0개의 댓글