
class
__init__
- 객체가 만들어질 때 자동으로 실행되어 객체의 초기 상태를 설정하는 메서드
- 미사용시에는 객체 속성을 직접 지정해야 하므로 코드가 지저분하고 실수하기 쉬움
- 장점
- 필수 속성을 강제하고 유효성 검증을 걸 수 있음
- 인스턴스마다 독립적인 상태를 보장해서 공유 버그(특히 리스트/딕셔너리 같은 가변 객체)들을 예방
__init__ 없는 경우
# 1. 기본구조
class 클래스 이름:
def 매서드(self): # self : 초기화
코드
class UserNoInit:
def greet(self):
return f"안녕하세요, 저는 {self.name}입니다."
u = UserNoInit()
print(u.greet())
__init__ 있는 경우
# 1. 기본구조
class 클래스이름:
def __init__(self):
self.속성 = 값
# 2. 기본구조2
class 클래스이름:
def __init__(self, 매개변수1, 매개변수2):
self.속성1 = 매개변수1
self.속성2 = 매개변수2
class UserInit:
def __init__(self, name, age):
if not name:
raise ValueError("이름은 비어 있을 수 없습니다.")
if age <= 0:
raise ValueError("나이는 1살 이상이어야 합니다.")
self.name = name
self.age = age
def greet(self):
return f"안녕하세요, 저는 {self.name}({self.age})입니다."
u = UserInit("기훈", 25)
print(u.greet())
class Person:
def __init__(self, name, age, address):
self.hello = '안녕하세요.'
self.name = name
self.age = age
self.address = address
def greeting(self):
print('{0} 저는 {1}입니다.'.format(self.hello, self.name))
maria = Person('마리아', 20, '서울시 서초구 반포동')
maria.greeting()
print('이름:', maria.name)
print('나이:', maria.age)
print('주소:', maria.address)
인스턴스별 독립 상태의 차이
class CartNoInit:
items = []
c1 = CartNoInit()
c2 = CartNoInit()
c1.items.append("사과")
print("c1:", c1.items)
print("c2:", c2.items)
class CartInit:
def __init__(self):
self.items = []
c1 = CartInit()
c2 = CartInit()
c1.items.append("사과")
print("c1:", c1.items)
print("c2:", c2.items)
클래스 상속
- 부모 클래스(Parent, Superclass)의 속성과 메서드를
- 자식 클래스(Child, Subclass)가 그대로 물려받아 사용할 수 있는 기능
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"안녕하세요, 저는 {self.name}이고 {self.age}살입니다.")
class Student(Person):
def __init__(self, name, age, school):
super().__init__(name, age)
self.school = school
def study(self):
print(f"{self.name}은(는) {self.school}에서 공부 중입니다.")
p1 = Person("기훈", 25)
p1.introduce()
s1 = Student("민수", 18, "OZ 코딩스쿨")
s1.introduce()
s1.study()
super().__init__(name, age)
- Person의 init이 실행되어 name과 age를 초기화
- Student("기훈", 25, "OZ코딩스쿨") 이 값이 들어가면
- 그중 name, age는 super().init(name, age)로 부모에게 전달
- 부모가 self.name, self.age를 설정
- def init(self, school):
- 이러면 self.name, self.age가 설정되지 않아서 Student("OZ")에는 name이 아예 존재X
오버라이딩(Overriding)
- 부모 메서드를 자식이 같은 이름으로 다시 정의해서 동작을 바꿀 수도 있다.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"안녕하세요, 저는 {self.name}이고 {self.age}살입니다.")
class Student(Person):
def introduce(self):
print(f"저는 학생 {self.name}이고, 나이는 {self.age}살이에요!")
s1 = Student("민수", 18)
s1.introduce()