μ½λ - ν¨μΊ μμ μ½λ μ°Έκ³ (ν¨μΊ μμ μ 리)
<μ΄μ κΈ>
https://silvercoding.tistory.com/21
[python μ¬ν] 4. λ°μ΄ν° λͺ¨λΈ(Data Model), Nametuple
μ½λ - ν¨μΊ μμ μ½λ μ°Έκ³ (ν¨μΊ μμ μ 리) <μ΄μ κΈ> https://silvercoding.tistory.com/20 https://silvercoding.tistory.com/19 https://silvercoding.tistory.com/18 [python μ¬ν] 1. κ°μ²΄μ§ν₯(OOP), ν΄..
silvercoding.tistory.com
<λ§€μ§ λ©μλ κ΄λ ¨ λ¬Έμ>
https://docs.python.org/3/reference/datamodel.html#special-method-names
3. Data model — Python 3.9.6 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
https://www.tutorialsteacher.com/python/magic-methods-in-python
Magic or Dunder Methods in Python
Python - Magic or Dunder Methods Magic methods in Python are the special methods that start and end with the double underscores. They are also called dunder methods. Magic methods are not meant to be invoked directly by you, but the invocation happens inte
www.tutorialsteacher.com
νμ΄μ¬μ μ€μν ν΅μ¬ νλ μ μν¬ : μνμ€(Sequence), λ°λ³΅(Iterator), ν¨μ(Functions), ν΄λμ€(Class)
- 맀μ§λ©μλ
n = 100
print(dir(n)) # λͺ¨λ μμ± λ° λ©μλ μΆλ ₯
__xxx__ ----> λ§€μ§ λ©μλ
*** μ¬μ©
print('EX1-1 - ', n + 200)
print('EX1-2 - ', n.__add__(200))
print('EX1-3 - ', n.__doc__)
print('EX1-4 - ', n.__bool__(), bool(n))
print('EX1-5 - ', n * 100, n.__mul__(100))
λ§€μ§ λ©μλλ νμ΄μ¬μμ μ΄λ―Έ μ μλ λ©μλλΌκ³ 보면 λλ€. μ΄λ³΄λ€λ λ λ€μν λ§€μ§ λ©μλλ€μ΄ μλ€.
μ΄λ¬ν μ μν νμ μ κ°μ²΄λ κ·Έλ₯ + , * μ΄λ¬ν κΈ°νΈλ€λ‘ μ¬μ©ν μ μκ² μ§λ§, μ§μ ν΄λμ€λ₯Ό λ§λ€μ΄ μ¬μ©ν λλ μ§μ μ μλ₯Ό ν΄μ μ μ©νκ² μ¬μ©ν μ μλ€.
- ν΄λμ€ λ§€μ§ λ©μλ μμ
(1) ν΄λμ€ μμ±
class Student:
# λ§€μ§ λ©μλμ. νμ΄μ¬μμ μ΄λ―Έ μ μλ λ©μλ
def __init__(self, name, height):
self._name = name
self._height = height
def __str__(self):
return 'Student Class Info : {}, {}'.format(self._name, self._height)
def __ge__(self, x):
print("Called >> __ge__ Method.")
if self._height >= x._height:
return True
else:
return False
def __le__(self, x):
print("Called >> __le__ Method")
if self._height <= x._height:
return True
else:
return False
def __sub__(self, x):
print('Called >> __sub__ Method.')
return self._height - x._height
μμ λ¬Έμμμ κ°μ Έμ¨ λ§€μ§ λ©μλ μλ―Έ
__str__ ---> To get called by built-int str() method to return a string representation of a type.
__le__ ---> To get called on comparison using <= operator.
__ge__ ---> To get called on comparison using >= operator.
__sub__ ---> To get called on subtraction operation using - operator.
(2) μ¬μ©
*** μΈμ€ν΄μ€ μμ±
s1 = Student('James', 181)
s2 = Student('Mie', 165)
Student ν΄λμ€μ μΈμ€ν΄μ€ μμ±!
*** λ§€μ§ λ©μλ μΆλ ₯
print('EX2-1 - ', s1 >= s2)
print('EX2-2 - ', s1 <= s2)
print('EX2-3 - ', s1 - s2)
print('EX2-4 - ', s2 - s1)
print('EX2-5 - ', s1)
print('EX2-6 - ', s2)
ν΄λμ€μ 맀μ§λ©μλλ₯Ό μ μνμ§ μμλ€λ©΄ κ°μ²΄μ κ°μ²΄κ°μ λΉκ΅λ‘ λ°μλ€μ΄κΈ° λλ¬Έμ μ€λ₯κ° λλ€.
ν΄λμ€μμ μ§μ μ μλ₯Ό ν΄λ¨μΌλ―λ‘ μ€λ₯κ° λμ§ μκ³ μ μν λλ‘ λμ¨λ€.
μ΄μκ°μ΄ 맀μ§λ©μλλ₯Ό μ§μ μ μνμ¬ μ μ©ν νμ©μ΄ κ°λ₯νλ€.
- ν΄λμ€ λ©μ§λ©μλ μ¬μ© μμ 2
(1) ν΄λμ€ μμ±
class Vector(object):
def __init__(self, *args):
'''Create a vector, 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 information'''
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, t):
'''Returns the vector addition of self and other'''
return Vector(self._x * t, self._y * t)
def __bool__(self):
return bool(max(self._x, self._y))
벑ν°λ₯Ό μμ±ν΄μ£Όλ ν΄λμ€μ΄λ€.
__add__λ©μλλ 벑ν°λΌλ¦¬ κ° μ’νμ λν λ§μ , __mul__λ©μλλ μ€μλ₯Ό μΈμμ λ£μ΄μ£Όλ©΄ κ° μ’νμ κ³±ν΄μ§λ κ²μ ꡬνν κ²μ΄λ€. __bool__λ©μλλ x, y μ’ν μ€ ν° κ°μ΄ 0μ΄ μλλ©΄ Trueλ₯Ό λ°ννκ² ν΄μ€λ€. (μ λ§ κ΅¬ννκ³ μΆμ λλ‘ κ΅¬ννλ©΄ λλ€!)
(2) μ¬μ©
# Vector μΈμ€ν΄μ€ μμ±
v1 = Vector(3, 5)
v2 = Vector(15, 20)
v3 = Vector()
# λ§€μ§ λ©μλ μΆλ ₯
print("EX3-1 - ", Vector.__init__.__doc__)
print("EX3-2 - ", Vector.__repr__.__doc__)
print("EX3-3 - ", Vector.__add__.__doc__)
print("EX3-4 - ", v1, v2, v3)
print("EX3-5 - ", v1 + v2)
print("EX3-6 - ", v1 * 4)
print("EX3-7 - ", v2 * 10)
print('EX3-8 - ', bool(v1), bool(v2))
print('EX3-9 - ', bool(v3))
__doc__ λ₯Ό μ¬μ©νλ©΄ μμμ μμ±νλ μ£Όμ μ€λͺ μ΄ μΆλ ₯λλ€. λλ¨Έμ§λ μ μ€νλλ©΄ μ±κ³΅!