17.7 연산자 오버로딩
몇 가지 특별한 메서드를 정의하는 것으로 프로그래머 정의 타입의 연산자가 어떻게 동작할지 지정할 수 있다. 예를 들어 Time 클래스에 _ _add_ _라는 메서드를 정의하면 Time 객체에 + 연산자를 사용할 수 있다.
_ _add_ _ 메서드는 다음과 같이 작성할 수 있다.
# class Time: 정의 안쪽
def _ _add_ _(self, other):
seconds = self.time_to_int() + other.time_to_int()
return int_to_time(seconds)
이제 + 연산자를 사용해보자.
>>> start = Time(9, 45)
>>> duration = Time(1, 35)
>>> print(start + duration)
11:20:00