๐ ์ด ํฌ์คํ ์์๋ Python์ special method์ ๋ํด ์ต์ํด์ง๋ ์๊ฐ์ ๊ฐ์ ธ๋ณด๊ฒ ์ต๋๋ค.
๐ฅ Special method๋?
๐ฅ Special method๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ๊ธฐ
๐ฅ Special method๋ก Vector ๋ง๋ค๊ธฐ
โ๏ธ ํ์ด์ฌ์ ๋ชจ๋ ์๋ฃํ์ ๋ฐ์ดํฐ ๊ตฌ์กฐ์์ ๊ฐ์ฒด๋ก ์ทจ๊ธํ๊ธฐ ๋๋ฌธ์ class ํํ๋ฅผ ๊ฐ์ง๊ณ ์๊ณ , ๋ฐ์ดํฐ ํ์
๋ณ๋ก ์ ์ฉํ ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค.
โ๏ธ ์๋ฃ๊ตฌ์กฐ์ ์ดํด๋ณด๋ฉด ๋๋ธ ์ธ๋์ค์ฝ์ด(__
)๋ก ์์ํ๊ณ ๋๋๋ ํํ์ ๋งค์๋๊ฐ ์กด์ฌํ๋๋ฐ ์ด๋ฅผ "Magic method" ๋๋ "Special method"๋ผ ๋ถ๋ฆ
๋๋ค.
print(int) # <class 'int'> print(dir(int)) """ ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes'] """
โ๏ธ ์๋ฅผ๋ค์ด, intํ ๋ฐ์ดํฐ๊ฐ +(๋ํ๊ธฐ), -(๋นผ๊ธฐ) ๋ฑ ์ฌ์น์ฐ์ฐ์ด ๊ฐ๋ฅํ ๊ฒ์ intํ ๋ฐ์ดํฐ ๊ตฌ์กฐ๊ฐ์๋ ์ด๋ฅผ ์ํ "Special method"๊ฐ ์ค๋น๋์๊ธฐ ๋๋ฌธ์ ๋๋ค.
# ๋ํ๊ธฐ(+) n = 100 print(n+200) # 300 print(n.__add__(200)) # 300 print(n.__doc__) """ int([x]) -> integer int(x, base=10) -> integer Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero. If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by '+' or '-' and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >> int('0b100', base=0) 4 """ # ๊ณฑํ๊ธฐ(*) n = 2 print(n*100) # 200 print(n.__mul__(100)) # 200 # Boolean n = 0 print(bool(n)) # False print(n.__bool__()) # False
โ๏ธ Calss ๊ฐ์ฒด๋ intํ์ฒ๋ผ ์ฌ์น์ฐ์ฐ์ด๋ ๋์๋น๊ต ๋ฑ์ ์๋ํ๋ฉด TypeError๊ฐ ๋ฐ์๋ฉ๋๋ค.
class Student: def __init__(self, name, height): self._name = name self._height = height # ๊ฐ์ฒด ์์ฑ s1 = Student('Jaewon', 171) s2 = Student('Haezin', 163) print(s1 + s2) # TypeError print(s1 >= s2) # TypeError print(s1 <= s2) # TypeError print(s1 - s2) # TypeError
โ๏ธ ์ด๋ดใท ๋, "Special method"๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ์ฌ ์ฌ์ฉํ๋ฉด ํ์์ ๋ฐ๋ผ ๊ทธ ๊ธฐ๋ฅ์ ์ ๊ณตํ ์ ์๊ณ , ์๋ ๋ฐฉ๋ฒ์ ๋ค๋ฅด๊ฒ customํ ์๋ ์์ต๋๋ค.
class Student: def __init__(self, name, height): self._name = name self._height = height # __str__ ์ค๋ฒ๋ผ์ด๋ฉ def __str__(self): return 'Student Class Info : {}, {}'.format(self._name, self._height) # __ge__ ์ค๋ฒ๋ผ์ด๋ฉ def __ge__(self, x): # ๐ ge : ํฌ๊ฑฐ๋ ๊ฐ๋ค if self._height >= x._height: return True else: return False # __le__ ์ค๋ฒ๋ผ์ด๋ฉ def __le__(self, x): # ๐ le : ์๊ฑฐ๋ ๊ฐ๋ค if self._height <= x._height: return True else: return False # __sub__ ์ค๋ฒ๋ผ์ด๋ฉ def __sub__(self, x): return self._height - x._height # ๊ฐ์ฒด ์์ฑ s1 = Student('Jaewon', 171) s2 = Student('Haezin', 163) # __str__ ๋งค์๋ ์ถ๋ ฅ print(s1) # Student Class Info : Jaewon, 171 print(s2) # Student Class Info : Haezin, 163 # __ge__ ๋งค์๋ ์ถ๋ ฅ print(s1 >= s2) # True print(s1.__ge__(s2)) # True # __le__ ๋งค์๋ ์ถ๋ ฅ print(s1 <= s2) # False print(s1.__le__(s2)) # False # __sub__ ๋งค์๋ ์ถ๋ ฅ print(s1 - s2) # 8 print(s2 - s1) # -8
# Magic Method class Vector(object): def __init__(self, *args): """Create a vetor, example : v = Vector(1,2)""" if len(args) == 0: self._x, self._y = 0, 0 else: self._x, self._y = args def __repr__(self): """Returns the vector informations""" return 'Vector(%r, %r)' % (self._x, self._y) def __add__(self, other): """Returns the vector addition of self and other""" return Vector(self._x + other._x, self._y + other._y) def __mul__(self, y): return Vector(self._x * y, self._y * y) def __bool__(self): return bool(max(self._x, self._y)) # Vector ์ธ์คํด์ค ์์ฑ v1 = Vector(3,5) v2 = Vector(15,20) v3 = Vector() # ๋งค์ง๋ฉ์๋ ์ถ๋ ฅ print(Vector.__init__.__doc__) # Create a vetor, example : v = Vector(1,2) print(Vector.__repr__.__doc__) # Returns the vector informations print(Vector.__add__.__doc__) # Returns the vector addition of self and other print(v1, v2, v3) # Vector(3, 5) Vector(15, 20) Vector(0, 0) print(v1 + v2) # Vector(18, 25) print(v1 * 4) # Vector(12, 20) print(v2 * 10) # Vector(150, 200) print(bool(v1), bool(v2), bool(v3)) # True True False