본문 바로가기
C++,python (인프런+사이트)/python 파이썬 정리

매직 메소드_오퍼레이터같은

by 알 수 없는 사용자 2022. 3. 31.

https://docs.python.org/3/reference/datamodel.html

 

3. Data model — Python 3.10.4 documentation

A class can implement certain operations that are invoked by special syntax (such as arithmetic operations or subscripting and slicing) by defining methods with special names. This is Python’s approach to operator overloading, allowing classes to define

docs.python.org

what is python special method doc 라고치면 나오는 파이썬의 수학의정석임


+ - * / % < > =< >= 등등 다있음

 

결론 : 매직메소드 는 C++ 오퍼레이터랑 비슷한 개념

 

 


PS>

인수 받아 올 때 이런식으로 받아오고 

예외처리 해주는 방식 잘봐둘 것. 

*은 포인터 X , 파이썬에선 '가변'이라는 뜻

이렇게 Vector() 해도 x,y에 0,0이 들어가게끔 예외처리가 가능함. ( 25,78,42 ) 이런건 안되고


위에처럼 

self._x, self._y = args 같은 정의 코드만 없으면

밑에 처럼 가변으로 인수를 받아 올 수 있다.

 

 

 

 

 

 

 

 

 

 

댓글