-
Python Class - 3Programming/python 2021. 6. 24. 22:19728x90
왜 클래스의 메소드 앞에는 self가 붙을까?
class Student(): # 클래스 변수 student_count = 0 def __init__(self, name, number): # 인스턴스 변수 self._name = name self._number = number Student.student_count += 1 def __str__(self): return 'str {}'.format(self._name) def __repr__(self): return 'repr {}'.format(self._name) def detail_info(self): print('current id = {}'.format(id(self))) print('Student detail info : {} {}'.format(self._name, self._number)) def __del__(self): Student.student_count -= 1
위의 클래스에서 detail_info 함수는 매번 객체 생성 후에만 호출이 가능할까?
# 클래스 인스턴스 메소드를 이렇게 호출해도 된다. Student.detail_info(student1) # Student detail info : Choi 1
위와 같이 호출해도 가능하다. self에 객체를 넘겨주면 된다.
'Programming > python' 카테고리의 다른 글
Python Class - 5 (0) 2021.06.29 Python Class - 4 (0) 2021.06.29 Python Class - 2 (0) 2021.06.24 Python Class - 1 (0) 2021.06.24 다익스트라(Dijkstra) 알고리즘 (0) 2021.03.10