{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "***\n", "# 20. 클래스와 연산자 중복 정의\n", "***\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "## 1 연산자 중복 (Operator Overloading)\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1-1 수치 연산자 중복\n", "- 직접 정의하는 클래스 인스턴스에 연산자를 적용하기 위하여 미리 약속되어 있는 메소드들을 정의" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
버전 | \n",
"메소드(Method) | \n",
"연산자(Operator) | \n",
"인스턴스 o에 대한 사용 예 | \n",
"
Python2.x/3.x | \n",
"__add__(self, B) | \n",
"+ (이항) | \n",
"o + B, o += B | \n",
"
Python2.x/3.x | \n",
"__sub__(self, B) | \n",
"- (이항) | \n",
"o - B, o -= B | \n",
"
Python2.x/3.x | \n",
"__mul__(self, B) | \n",
"* | \n",
"o * B, o *= B | \n",
"
Python2.x | \n",
"__div__(self, B) | \n",
"/ | \n",
"o / B, o /= B | \n",
"
Python3.x | \n",
"__truediv__(self, B) | \n",
"/ | \n",
"o / B, o /= B | \n",
"
Python2.x | \n",
"__rdiv__(self, B) | \n",
"/ | \n",
"B / o | \n",
"
Python3.x | \n",
"__rtruediv__(self, B) | \n",
"/ | \n",
"B / o | \n",
"
Python2.x/3.x | \n",
"__floordiv__(self, B) | \n",
"// | \n",
"o // B, o //= B | \n",
"
Python2.x/3.x | \n",
"__mod__(self, B) | \n",
"% | \n",
"o % B, o %= B | \n",
"
Python2.x/3.x | \n",
"__divmod__(self, B) | \n",
"divmod() | \n",
"divmod(o, B) | \n",
"
Python2.x/3.x | \n",
"__pow__(self, B) | \n",
"pow(), ** | \n",
"pow(o, B), o ** B | \n",
"
Python2.x/3.x | \n",
"__lshift__(self, B) | \n",
"<< | \n",
"o << B, o <<= B | \n",
"
Python2.x/3.x | \n",
"__rshift__(self, B) | \n",
">> | \n",
"o >> B, o >>= B | \n",
"
Python2.x/3.x | \n",
"__and__(self, B) | \n",
"& | \n",
"o & B, o &= B | \n",
"
Python2.x/3.x | \n",
"__xor__(self, B) | \n",
"^ | \n",
"o ^ B, o ^= B | \n",
"
Python2.x/3.x | \n",
"__or__(self, B) | \n",
"| | \n",
"o | B, o |= B | \n",
"
Python2.x/3.x | \n",
"__neg__(self) | \n",
"- (단항) | \n",
"-A | \n",
"
Python2.x/3.x | \n",
"__abs__(self) | \n",
"abs() | \n",
"abs(o) | \n",
"
Python2.x/3.x | \n",
"__pos__(self) | \n",
"+ (단항) | \n",
"+o | \n",
"
Python2.x/3.x | \n",
"__invert__(self) | \n",
"~ | \n",
"~o | \n",
"
참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일
" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 1 }