패캠을 수료한 지도 한 달이 지났다.
6개월 간 강의가 몇 개 제공되는데 하나를 쭉 제대로 듣는게 중요하다는 생각이 들어 강의 몇 가지를 둘러보다가 하나를 정했다.
한 번에 끝내는 딥러닝/인공지능 초격차 패키지
강의 시간이 꽤 길다 보니 리뷰도 오래 걸릴 것 같은데
기초 파이썬과 수학부터 정리를 해보려고 한다.
대부분 아는 내용이지만 클래스는 아직 잘 사용하지 않고 개념이 흐릿한 생각이 들어 클래스에 대해서 다시 복습하고 정리하려 한다.
(1) 클래스
실세계의 것을 모델링해 속성(attribute)와 동작(method)를 갖는 데이터 타입
클래스는 새로운 타입을 정의하고, 정의된 타입에서 구체화된 인스턴스를 만드는 것이 객체 생성
1) class 선언하기
class Person :
pass
bob = Person()
아무 method가 없는 클래스를 생성
bob은 Person이라는 클래스로 타입이 지정된다.
2) __init__(self)
생성자, 클래스 인스턴스가 생성될 때 호출됨
항상 첫번째에 온다.
class Person :
def __init__(self):
#속성 정의
self.name = 'Kate'
self.age = 10
정의 된 속성은 외부에서 변경될 수도 있다.
어떤 Person 객체를 생성하더라도 같은 속성이 정의된다.
이때 동적으로 속성을 정의한다면
class Person :
def __init__(self, name, age):
#속성 정의
self.name = 'Kate'
self.age = 10
p1 = Person('Bob', 30) 으로
속성 부여가 가능
3) method
멤버 함수라고도 하며 해당 클래스의 object에서만 호출 가능
객체 레벨에서 호출되며 해당 객체의 속성에 대한 연산
{obj}.{method}()형태로 호출
class Countter:
def __init__(self):
self.num = 0
def print_current_value(self):
print('현재 값은 :', self.num)
c1 = Counter()
c1.print_current_value()
결과 --> 현재 값은 : 0
이때 기능을 숫자 하나 증가, 숫자 0으로 초기화
두 가지로 나눈다면
class Countter:
def __init__(self):
self.num = 0
def increment(self):
self.num += 1
def reset(self):
self.num = 0
def print_current_value(self):
print('현재 값은 :', self.num)
c1 = Counter()
c1.increment()
c1.print_current_value()
c1.reset()
c1.print_current_value()
결과 -->
현재 값은 : 1
현재 값은 : 0
method type
instance method - 객체로 호출
class method(static) - class로 호출
class Math:
def add(self, a, b):
return a + b
def multiply(self, a, b):
return a * b
m = Math()
m.add(10, 20)
m.multiply(10, 20)
이때는 속성이 없어서 self가 할 것이 없음
--> 내부에 유지할 것이 없는 경우 instance 레벨로 할 필요가 없다. = staticmethod 표현
class Math:
@staticmethod
def add(self, a, b):
return a + b
@staticmethod
def multiply(self, a, b):
return a * b
Math.add(10, 20)
Math.multiply(10, 20)
위와 같은 식으로 사용하면 된다.
4) 상속 (Inheritance)
기존 정의해둔 클래스 기능을 그대로 물려받는 기능
의미적으로 is-a 관계를 갖는다.
예시)
#부모클래스
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print('{}은 {}를 먹습니다.'.format(self.name, food))
def sleep(self, minute):
print('{}은 {}분 동안 잡니다.'.format(self.name, minute))
def work(self, minute):
print('{}은 {}분 동안 일합니다.'.format(self.name, minute))
#자녀클래스
class Student(Person):
def __init__(self, name, age):
self.name = name
self.age = age
class Employee(Person):
def __init__(self, name, age):
self.name = name
self.age = age
bob = Student('bob', 25)
bob.eat('BBQ')
bob.sleep(30)
bob.work(60)
Student, Employee 가 Person 함수를 상속 받아서 해당 함수가 없더라도 Person의 함수를 받아 이용
4.1 Method Override
#부모클래스
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print('{}은 {}를 먹습니다.'.format(self.name, food))
def sleep(self, minute):
print('{}은 {}분 동안 잡니다.'.format(self.name, minute))
def work(self, minute):
print('{}은 {}분 동안 일합니다.'.format(self.name, minute))
#자녀클래스
class Student(Person):
def __init__(self, name, age):
self.name = name
self.age = age
def work(self, minute):
print('{}은 {}분 동안 공부합니다.'.format(self.name, minute))
class Employee(Person):
def __init__(self, name, age):
self.name = name
self.age = age
bob = Student('bob', 25)
bob.eat('BBQ')
bob.sleep(30)
bob.work(60)
work를 Student에서 공부합니다로 변경.
특정한 경우에만 부모 클래스의 함수를 변경해서 원하는 대로 수정할 수 있다. = 재정의 후 부모클래스 기능 배제
4.2 super
override는 기존 함수를 변경하는 것이지만 super는 그대로 가져오는 경우 = 위에 함수가 유지 되면서 자녀 클래스에서 새롭게 사용
#부모클래스
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print('{}은 {}를 먹습니다.'.format(self.name, food))
def sleep(self, minute):
print('{}은 {}분 동안 잡니다.'.format(self.name, minute))
def work(self, minute):
print('{}은 {}분 동안 준비를 합니다.'.format(self.name, minute))
#자녀클래스
class Student(Person):
def __init__(self, name, age):
self.name = name
self.age = age
def work(self, minute):
super().work(minute)
print('{}은 {}분 동안 공부합니다.'.format(self.name, minute))
class Employee(Person):
def __init__(self, name, age):
self.name = name
self.age = age
def work(self, minute):
super().work(minute)
print('{}은 {}분 동안 업무를 합니다.'.format(self.name, minute))
bob = Student('bob', 25)
bob.eat('BBQ')
bob.sleep(30)
bob.work(60)