[Python] Special Method

최대 1 분 소요

🙄 객체를 만들어 사용하다보면 객체끼리의 연산, 비교 등이 필요한 경우가 있다. 자신이 원하는 방식으로 객체의 연산과 비교를 진행할 수 있도록 만들어주는 것이 바로 스페셜 메서드 / 매직 메서드이다!


👩 스페셜 메서드(Special method)?

  • 매직 메서드(Magic method)라고도 하며 파이썬 객체들이 동일하게 가지는 인터페이스로 이해할 수 있다.
  • 객체.메서드()의 형식으로 호출된다.
  • 앞, 뒤에 두 개의 언더바가 들어가 던더 메서드(dunder method)라고도 한다.


🙄 그럼 지금부터 몇 가지 스페셜 메서드들을 알아봅시다!


🧐 표준 파이썬 시퀀스 스페셜 메서드

  class Fruit:
    def __init__(self, name, types): # 1. __init__()
      self.name = name
      self.types = types

    def __len__(self): # 2. __len__()
      return len(self.types)

    def __getitem__(self, position): # 3. __getitem__()
      return self.types[position]


🧐 수치형 스페셜 메서드

    # 덧셈
    def __add__(self, other):
      return len(self.types) + len(other.types)

    # 뺄셈
    def __sub__(self, other):
      ...

    # 곱셈
    def __mul__(self, other):
      ...
    
    # 나눗셈
    def __mul__(self, other):
      ...


🧐 비교를 위한 스페셜 메서드

  # <
  def __lt__(self, other):
    return len(self.types) < len(other.types)

  # <=
  def __le__(self, other):
    ...

  # >
  def __gt__(self, other):
    ...

  # >=
  def __ge__(self, other):
    ...

  # ==
  def __eq__(self, other):

  # !=
  def __ne__(self, other):
  • 위의 메소드들을 구현한다면 객체 사이의 비교가 가능해집니다. 따라서 객체들로 이루어진 리스트정렬 또한 가능해집니다!


📃 참고

댓글남기기