반응형

프로토콜 (Protocol)

프로토콜은 특정 메소드나 속성을 구현해야 하는 비공식적인 인터페이스입니다. Python은 강력한 덕 타이핑(duck typing) 덕분에 공식적으로 인터페이스를 정의하지 않아도 됩니다. 객체가 특정 메소드나 속성을 가지고 있다면, 그 객체는 해당 프로토콜을 구현한다고 간주합니다.

예를 들어, Python의 시퀀스 프로토콜은 __len__과 __getitem__ 메소드를 요구합니다. 리스트, 튜플, 문자열 등은 모두 이 프로토콜을 구현합니다.

 

 

인터페이스를 정의한다는 것은 클래스가 가져야 할 메소드와 속성을 명시하는 것을 의미합니다. 이는 객체 지향 프로그래밍에서 매우 중요한 개념입니다. 인터페이스는 클래스가 특정한 기능을 제공할 것이라는 계약(Contract)을 정의하며, 이를 통해 코드의 일관성을 유지하고 재사용성을 높일 수 있습니다.

인터페이스의 개념

인터페이스는 일반적으로 다음을 포함합니다:

  • 메소드 서명(Signature): 메소드의 이름, 매개변수, 반환 타입 등을 명시합니다.
  • 속성(Property): 클래스가 가져야 할 속성을 정의합니다.

인터페이스는 구현을 포함하지 않으며, 인터페이스를 상속받는 클래스는 해당 인터페이스에서 정의된 모든 메소드와 속성을 구현해야 합니다.

 

class CustomSequence:
    def __len__(self):
        return 10
    
    def __getitem__(self, index):
        return index * 2

seq = CustomSequence()
print(len(seq))        # 10
print(seq[3])          # 6

 

abc 모듈 (추상 기반 클래스)

Python의 abc 모듈은 추상 기반 클래스를 정의하는 기능을 제공합니다. 추상 클래스는 하나 이상의 추상 메소드를 포함할 수 있으며, 이러한 메소드는 서브클래스에서 반드시 구현해야 합니다. 이를 통해 공통 인터페이스를 정의하고 구현 강제를 할 수 있습니다.

추상 클래스 정의

추상 클래스는 ABC 클래스를 상속받고, 추상 메소드는 @abstractmethod 데코레이터를 사용하여 정의합니다.

 

from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

class Dog(Animal):
    def sound(self):
        return "Bark"

 

추상 클래스의 역할

  1. 공통 인터페이스 정의: 추상 클래스는 여러 구체적인 클래스가 반드시 구현해야 하는 메소드와 속성을 정의합니다. 이를 통해 모든 서브클래스가 동일한 인터페이스를 가지게 됩니다.
  2. 코드 재사용성 증가: 추상 클래스는 공통 기능을 한 곳에 모아서 구현할 수 있습니다. 이를 상속받는 구체적인 클래스들은 이러한 공통 기능을 재사용할 수 있습니다.
  3. 유지보수성 향상: 코드를 수정할 때, 공통 기능은 추상 클래스에서 한 번만 수정하면 되므로 유지보수성이 높아집니다.
  4. 다형성 지원: 동일한 인터페이스를 통해 다양한 클래스와 상호작용할 수 있게 하여 다형성을 지원합니다.

 

from abc import ABC, abstractmethod

class Animal(ABC):
    def __init__(self, name):
        self.name = name

    @abstractmethod
    def sound(self):
        return "Some generic animal sound"

    @abstractmethod
    def move(self):
        return "Some generic animal movement"

    def display_info(self):
        print(f"{self.name} says {self.sound()} and {self.move()}")

class Dog(Animal):
    def sound(self):
        return "Bark"

    def move(self):
        return "Runs"

class Cat(Animal):
    def sound(self):
        return "Meow"

    # move 메소드를 재정의하지 않음

class Fish(Animal):
    # sound 메소드를 재정의하지 않음

    def move(self):
        return "Swims"

# 동물원 관리 시스템
class Zoo:
    def __init__(self):
        self.animals = []

    def add_animal(self, animal: Animal):
        self.animals.append(animal)

    def show_all_animals(self):
        for animal in self.animals:
            animal.display_info()

# 동물원에 동물 추가
zoo = Zoo()
zoo.add_animal(Dog("Buddy"))
zoo.add_animal(Cat("Whiskers"))
zoo.add_animal(Fish("Nemo"))

# 모든 동물 정보 표시
zoo.show_all_animals()

 

실행 결과

Buddy says Bark and Runs
Whiskers says Meow and Some generic animal movement
Nemo says Some generic animal sound and Swims

 

예제에서는 추상클래스 "Animal"을 선언하고 각각 Dog, Cat 그리고 Fish 클래스가 이를 상속하도록 했습니다.

 

구체적인 클래스 Zoo 에서 Dog, Cat 그리고 Fish 오브젝트를 생성하였으며 여기서 모든 동물들에게 상속받은 추상 클래스 display_info를 호출하면 실행 결과를 출력하게 됩니다.

 

 

추상클래스는 다중 상속도 가능합니다.

from abc import ABC, abstractmethod

class Printable(ABC):
    @abstractmethod
    def print(self):
        pass

class Scannable(ABC):
    @abstractmethod
    def scan(self):
        pass

class MultifunctionPrinter(Printable, Scannable):
    def print(self):
        return "Printing document"

    def scan(self):
        return "Scanning document"

# MultifunctionPrinter 클래스는 Printable과 Scannable의 추상 메소드를 모두 구현해야 합니다
mfp = MultifunctionPrinter()
print(mfp.print())  # "Printing document"
print(mfp.scan())   # "Scanning document"

 

MultifunctionPrinter에 Printable와 Scannable 추상 클래스를 다중 상속 하고 있습니다.

 

일반 메서드의 경우, 서브클래스에서 재정의하지 않으면 상속된 메서드를 그대로 사용할 수 있습니다. 즉, 서브클래스는 필요에 따라 메서드를 재정의할 수도 있고, 그렇지 않으면 상위 클래스에서 정의된 메서드를 사용할 수 있습니다.

 

그러나 추상 클래스에서 정의된 추상 메서드는 특별한 경우입니다. 추상 메서드는 기본적으로 구현되지 않은 메서드입니다. 따라서 추상 클래스에서 추상 메서드를 정의하면, 이를 상속받는 클래스는 반드시 그 추상 메서드를 재정의하여 구현해야 합니다. 그렇지 않으면 해당 서브클래스는 추상 클래스로 간주되어 인스턴스화할 수 없습니다.

 

from abc import ABC, abstractmethod

class AbstractClass(ABC):
    @abstractmethod
    def abstract_method(self):
        pass

    def concrete_method(self):
        print("This is a concrete method from the abstract class.")

class ConcreteClass(AbstractClass):
    def abstract_method(self):
        print("This is the implementation of the abstract method.")

# 인스턴스 생성
instance = ConcreteClass()
instance.abstract_method()  # This is the implementation of the abstract method.
instance.concrete_method()  # This is a concrete method from the abstract class.

 

추상 클래스의 목적은 인터페이스를 정의하고, 특정 메서드를 서브클래스에서 반드시 구현하도록 강제하는 것입니다. 이를 통해 코드의 일관성을 유지하고, 서브클래스가 필요한 기능을 올바르게 구현하도록 보장합니다.

 

 

 

직접 ABC를 정의할 수 있지만 가능하면 collections.abc나 파이썬 표준 라이브러리의 모듈에서 상속해서 써라!

 

 

반응형

'Computer Science > python' 카테고리의 다른 글

연산자 오버로딩  (0) 2024.07.09
추상클래스의 활용  (0) 2024.06.12
Python 데코레이터  (0) 2024.05.21
seaborn clustermap color label  (0) 2022.05.24
flask_sqlalchemy  (0) 2022.05.23

+ Recent posts