클래스 속성
- 클래스이름.클래스변수로 사용
- 클래스에 속해있으며 모든 인스턴스에서 공유
- 인스턴스 전체가 사용해야 하는 값을 저장할 때 사용
인스턴스 속성
- 생성자(__init__)에서 self.속성에 할당했던 변수들은 모두 인스턴스 속성에 해당
- 인스턴스별로 별개. 서로 영향을 받지 않음
- 각 인스턴스가 값을 따로 저장해야 할 때 사용
- 각각의 def 들은 모두 인스턴스 속성
정적메소드 속성
- 클래스 메서드는 정적 메서드처럼 인스턴스 없이 호출가능.
# 정적메소드 쓰기 전
class Person():
def __init__(self): # self 필요
self.hello='hi' # self 필요
print(self.hello) # self 필요
time.sleep(3)
def greeting(self,name,age): # self 필요
self.name = name # self 필요
self.age = int(age)+1 # self 필요
print('My name is {0},{1} years old '.format(self.name,self.age))
genius = Person()
genius.greeting('a',21)
---------------
hi
My name is a,22 years old
------------------------------------------------
# 정적메소드 쓴 후
class Person():
@staticmethod # 정적메소드
def __init__(): # self 필요없음
print('hi2') # self 필요없음
time.sleep(3)
@staticmethod # 정적메소드
def greeting(name): # self 필요없음
print('My name is {}'.format(name))
genius = Person()
genius.greeting('a')
728x90
'Study (Data Science) > Python' 카테고리의 다른 글
프로그래머스) 점의 위치 구하기 (0) | 2022.12.04 |
---|---|
프로그래머스) 배열 원소의 길이, 짝수 홀수 개수 (0) | 2022.12.02 |
Pandas, NumPy (0) | 2022.12.01 |
기본수학, numpy (3) | 2022.12.01 |
게임 캐릭터 클래스 만들기 (0) | 2022.11.30 |
댓글