### 클래스 메소드 & 스태틱 메소드
인스턴스를 통하지 않고, 클래스에서 직접 호출할 때 사용하는 메소드이다.
- 클래스 메소드: 메소드 위에 @classmethod를 기입해준다. 단, 첫번째 매개변수로 cls로 입력해준다. 클래스 메서드는 정적 메서드처럼 인스턴스 없이 호출할 수 있다는 점은 같지만 클래스 메서드는 메서드 안에서 클래스 속성, 클래스 메서드에 접근해야 할 때 사용한다.
- 스태틱 메소드: 메소드 위에 @staticmethod를 기입해준다. 메서드의 실행이 외부 상태에 영향을 끼치지 않는 순수 함수(pure function)를 만들 때 사용한다. 정적 메서드는 인스턴스의 상태를 변화시키지 않는 메서드를 만들 때 사용합니다.
```python
# Chapter01-3
# 파이썬 심화
# 클래스 메소드, 인스턴스 메소드, 스태틱 메소드
# 기본 인스턴스 메소드
class Student(object):
"""
Student Class
Author : Kim
Date : 2020.04.23
Descriptipn : Class, Static, Instance Method
"""
# Class Variable
tuition_per = 1.0
def __init__(self, id, first_name, last_name, email, grade, tuition, gpa):
self._id = id
self._first_name = first_name
self._last_name = last_name
self._email = email
self._grade = grade
self._tuition = tuition
self._gpa = gpa
# Instance Method - 인자로 self가 들어가면 인스턴스 메소드라 생각하면 될듯
def full_name(self):
return '{}, {}'.format(self._first_name, self._last_name)
# Instance Method
def detail_info(self):
return 'Student Detail Info : {}, {}, {}, {}, {}, {}'.format(self._id, self.full_name(), self._email, self._grade, self._tuition,
self._gpa)
# Instance Method
def get_fee(self):
return 'Before Tuition -> Id: {}, fee: {}'.format(self._id, self._tuition)
# Instance Method
def get_fee_calc(self):
return 'After Tuition -> Id: {}, fee: {}'.format(self._id, self._tuition * Student.tuition_per)
def __str__(self):
return 'Student Info -> name: {} grade: {} email: {}'.format(self.full_name(), self._grade, self._email)
# Class Method
@classmethod
def raise_fee(cls, per):
if per <= 1:
print('Please Enter 1 or More')
return
cls.tuition_per = per
print('Succed! tuition increased!')
# Class Method
@classmethod
def student_const(cls, id, first_name, last_name, email, grade, tuition, gpa):
return cls(id, first_name, last_name, email, grade, tuition * cls.tuition_per, gpa)
# Static Method
@staticmethod
def is_scholarship_st(inst):
if inst._gpa >= 4.3:
return '{} is a scholarship recipient'.format(inst._last_name)
return 'Sorry. Not a scholarship recipient.'
# 학생 인스턴스
student_1 = Student(1, 'Kim', 'Sarang', 'student1@naver.com', '1', 400, 3.5)
student_2 = Student(2, 'Lee', 'Myungho', 'student2@daum.com', '2', 500, 4.3)
# 기본정보
print(student_1)
print(student_2)
print()
# 전체 정보
print(student_1.detail_info())
print(student_2.detail_info())
print()
# 학비 정보(인상 전)
print(student_1.get_fee())
print(student_2.get_fee())
print()
# 학비 인상(클래스 메소드 사용 전) - 직접 접근해서 제어하면 좋지 않
# Student.tuition_per = 1.2
# 학비 인상(클래스 메소드 사용)
Student.raise_fee(1.3)
# 학비 정보(인상 후)
print(student_1.get_fee_calc())
print(student_2.get_fee_calc())
print()
# 클레스 메소드 인스턴스 생성 실습
student_3 = Student.student_const(3, 'Park', 'Minji', 'Student3@gmail.com', '3', 550, 4.5)
student_4 = Student.student_const(4, 'Cho', 'Sunghan', 'Student4@gmail.com', '4', 600, 4.1)
# 전체 정보
print(student_3.detail_info())
print(student_4.detail_info())
print()
# 학생 학비 변경 확인
print(student_3._tuition)
print(student_4._tuition)
print()
# 장학금 혜택 여부(스테이틱 메소드 미사용)
def is_scholarship(inst):
if inst._gpa >= 4.3:
return '{} is a scholarship recipient'.format(inst._last_name)
return 'Sorry. Not a scholarship recipient.'
print(is_scholarship(student_1))
print(is_scholarship(student_2))
print(is_scholarship(student_3))
print(is_scholarship(student_4))
print()
# 장학금 혜택 여부(스테이틱 메소드 사용)
print(Student.is_scholarship_st(student_1))
print(Student.is_scholarship_st(student_2))
print(Student.is_scholarship_st(student_3))
print(Student.is_scholarship_st(student_4))
print()
print(student_1.is_scholarship_st(student_1))
print(student_2.is_scholarship_st(student_2))
print(student_3.is_scholarship_st(student_3))
print(student_4.is_scholarship_st(student_4))