KR_Class - somaz94/python-study GitHub Wiki
ν΄λμ€λ κ°μ²΄λ₯Ό μμ±νκΈ° μν ν
νλ¦Ώμ΄λ€. λ°μ΄ν°μ μ΄λ₯Ό μ²λ¦¬νλ λ©μλλ₯Ό νλμ λ¨μλ‘ λ¬Άμ΄ κ΄λ¦¬ν μ μλ€.
# κΈ°λ³Έ ν΄λμ€ κ΅¬μ‘°
class Calculator:
# μμ±μ
def __init__(self):
self.result = 0
# λ©μλ
def add(self, num):
self.result += num
return self.result
β
νΉμ§:
- κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ°
- λ°μ΄ν°μ λ©μλ μΊ‘μν
- μ¬μ¬μ© κ°λ₯ν μ½λ
ν΄λμ€λ‘λΆν° μμ±λ μ€μ κ°μ²΄λ₯Ό μΈμ€ν΄μ€λΌκ³ νλ€.
# κ°μ²΄ μμ±
calc = Calculator() # calcλ Calculatorμ μΈμ€ν΄μ€
# κ°μ²΄ μ¬μ©
calc.add(5) # λ©μλ νΈμΆ
print(calc.result) # μμ± μ κ·Ό
β
νΉμ§:
- ν΄λμ€μ μ€μ²΄ν
- λ 립μ μΈ μν μ μ§
- λ©μλ μ κ·Ό κ°λ₯
μμ±μμ μλ©Έμλ κ°μ²΄μ μλͺ
μ£ΌκΈ°λ₯Ό κ΄λ¦¬νλ νΉμ λ©μλμ΄λ€.
class Person:
# μμ±μ
def __init__(self, name):
self.name = name
print(f"{name} κ°μ²΄κ° μμ±λ¨")
# μλ©Έμ
def __del__(self):
print(f"{self.name} κ°μ²΄κ° μ κ±°λ¨")
β
νΉμ§:
- κ°μ²΄ μ΄κΈ°ν
- 리μμ€ μ 리
- μλͺ μ£ΌκΈ° κ΄λ¦¬
μμμ κΈ°μ‘΄ ν΄λμ€μ μμ±κ³Ό λ©μλλ₯Ό μλ‘μ΄ ν΄λμ€κ° λ¬Όλ €λ°λ κΈ°λ₯μ΄λ€.
# λΆλͺ¨ ν΄λμ€
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
pass
# μμ ν΄λμ€
class Dog(Animal):
def speak(self):
return f"{self.name}κ° λ©λ©!"
# μμ ν΄λμ€
class Cat(Animal):
def speak(self):
return f"{self.name}κ° μΌμΉ!"
β
νΉμ§:
- μ½λ μ¬μ¬μ©
- λ©μλ μ€λ²λΌμ΄λ©
- λ€νμ± κ΅¬ν
μΊ‘μνλ ν΄λμ€ λ΄λΆμ λ°μ΄ν°λ₯Ό 보νΈνκ³ μΈλΆμμμ μ κ·Όμ μ ννλ κΈ°λ²μ΄λ€.
class BankAccount:
def __init__(self):
self.__balance = 0 # private λ³μ
# getter λ©μλ
def get_balance(self):
return self.__balance
# setter λ©μλ
def set_balance(self, amount):
if amount >= 0:
self.__balance = amount
β
νΉμ§:
- λ°μ΄ν° μλ
- μ κ·Ό μ μ΄
- μμ ν μν κ΄λ¦¬
ν΄λμ€ λ©μλμ μ μ λ©μλλ μΈμ€ν΄μ€κ° μλ ν΄λμ€ λ 벨μμ λμνλ λ©μλμ΄λ€.
class MyClass:
count = 0 # ν΄λμ€ λ³μ
def __init__(self):
MyClass.count += 1
@classmethod
def get_count(cls): # ν΄λμ€ λ©μλ
return cls.count
@staticmethod
def utility_method(): # μ μ λ©μλ
return "μ νΈλ¦¬ν° κΈ°λ₯"
β
νΉμ§:
- ν΄λμ€ λ 벨 κΈ°λ₯
- μΈμ€ν΄μ€ λ 립μ
- μ νΈλ¦¬ν° κΈ°λ₯
λ€νμ±μ λμΌν μΈν°νμ΄μ€λ₯Ό ν΅ν΄ λ€μν ννμ κ°μ²΄λ₯Ό μ²λ¦¬ν μ μλ λ₯λ ₯μ΄λ€.
def animal_speak(animal):
# λλ¬Ό κ°μ²΄κ° μ΄λ€ νμ
μ΄λ speak λ©μλλ₯Ό νΈμΆ
print(animal.speak())
# λ€μν κ°μ²΄λ‘ λμΌν ν¨μ νΈμΆ
dog = Dog("λ°λμ΄")
cat = Cat("λλΉ")
animal_speak(dog) # λ°λμ΄κ° λ©λ©!
animal_speak(cat) # λλΉκ° μΌμΉ!
β
νΉμ§:
- μ½λ μ μ°μ±
- μΈν°νμ΄μ€ κΈ°λ° νλ‘κ·Έλλ°
- νμ₯μ± μ¦κ°
νμ΄μ¬μμλ μμ± μ κ·Όμ 컀μ€ν°λ§μ΄μ¦νλ μ¬λ¬ λ°©λ²μ μ 곡νλ€.
class Person:
def __init__(self):
self._age = 0
# property λ°μ½λ μ΄ν°λ₯Ό μ¬μ©ν getter
@property
def age(self):
return self._age
# setter λ©μλ
@age.setter
def age(self, value):
if value >= 0:
self._age = value
# deleter λ©μλ
@age.deleter
def age(self):
del self._age
print("age μμ±μ΄ μμ λ¨")
# μ¬μ© μμ
person = Person()
person.age = 25 # setter νΈμΆ
print(person.age) # getter νΈμΆ
del person.age # deleter νΈμΆ
β
νΉμ§:
- μμ± μ κ·Ό μ μ΄
- κ³μ°λ μμ± κ΅¬ν
- μ κ·Ό λ‘μ§ μΊ‘μν
λ§€μ§ λ©μλλ μ΄λ¦ μλ€μ μ΄μ€ λ°μ€(__
)μ΄ μλ νΉμ λ©μλλ‘, νμ΄μ¬μ λ΄μ₯ μ°μ°μλ ν¨μμ μ°κ²°λλ€.
class Vector:
def __init__(self, x, y):
self.x = x
self.y = y
# λ²‘ν° λ§μ
μ°μ°μ (+)
def __add__(self, other):
return Vector(self.x + other.x, self.y + other.y)
# λ¬Έμμ΄ νν
def __str__(self):
return f"Vector({self.x}, {self.y})"
# κ°μ²΄ νν
def __repr__(self):
return f"Vector({self.x}, {self.y})"
# κΈΈμ΄(ν¬κΈ°) κ³μ°
def __len__(self):
return int((self.x**2 + self.y**2)**0.5)
# λΉκ΅ μ°μ°μ (==)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
# μ¬μ© μμ
v1 = Vector(2, 3)
v2 = Vector(3, 4)
v3 = v1 + v2 # __add__ νΈμΆ
print(v3) # __str__ νΈμΆ
print(len(v3)) # __len__ νΈμΆ
print(v1 == v2) # __eq__ νΈμΆ
β
μ£Όμ λ§€μ§ λ©μλ:
-
__init__
: κ°μ²΄ μ΄κΈ°ν -
__str__
,__repr__
: λ¬Έμμ΄ νν -
__add__
,__sub__
,__mul__
: μ°μ°μ μ€λ²λ‘λ© -
__len__
,__getitem__
,__setitem__
: μνμ€/λ§€ν λμ -
__enter__
,__exit__
: 컨ν μ€νΈ κ΄λ¦¬μ -
__call__
: νΈμΆ κ°λ₯ κ°μ²΄
νμ΄μ¬μ μ¬λ¬ ν΄λμ€λ₯Ό μμλ°μ μ μλ λ€μ€ μμμ μ§μνλ€.
# κΈ°λ³Έ ν΄λμ€
class Animal:
def speak(self):
pass
# λ―Ήμ€μΈ ν΄λμ€λ€
class SwimMixin:
def swim(self):
return "μμ μ€"
class FlyMixin:
def fly(self):
return "λΉν μ€"
# λ€μ€ μμ
class Duck(Animal, SwimMixin, FlyMixin):
def speak(self):
return "κ½₯κ½₯"
# μ¬μ© μμ
duck = Duck()
print(duck.speak()) # κ½₯κ½₯
print(duck.swim()) # μμ μ€
print(duck.fly()) # λΉν μ€
β
νΉμ§:
- μ¬λ¬ ν΄λμ€μ κΈ°λ₯ μ‘°ν©
- λ―Ήμ€μΈ ν¨ν΄μΌλ‘ μ½λ μ¬μ¬μ©
- λ€μ΄μλͺ¬λ λ¬Έμ μ MRO(Method Resolution Order)
β
λͺ¨λ² μ¬λ‘:
- ν΄λμ€λͺ μ νμ€μΉΌ μΌμ΄μ€(PascalCase) μ¬μ©
- μΈμ€ν΄μ€ λ©μλμ 첫 λ§€κ°λ³μλ νμ self
- private μμ±μ μ΄λ¦ μμ __ μΆκ°
- μμλ³΄λ€ μ»΄ν¬μ§μ μ νΈ
- μμ± μ κ·Όμλ νλ‘νΌν° μ¬μ©
- λ§€μ§ λ©μλλ‘ νμ΄μ¬μ κ·μ½ νμ©
- λ€μ€ μμμ μ μ€νκ² μ¬μ©
- λ―Ήμ€μΈμ ν κ°μ§ κΈ°λ₯μ μ§μ€
- docstringμΌλ‘ ν΄λμ€μ λ©μλ λ¬Έμν
- λΆλ³ κ°μ²΄λ
__hash__
μ__eq__
ꡬν