# 클래스 선언 및 self 의 이해
# 방대한 양은 클래스로 구조화를시키고 서로의 결합을 느슨하게해서 오류를 줄이고 성능을 높히는게 핵심임.
# 유지보수와 생산성을 위함임.
# 파이썬 클래스 상세 이해
# self , 클래스, instance 변수
# 선언
# class 클래스명:
# 함수
# 함수
# 함수
class Fist:
pass # 이거 해두면 일단 오류 안남 .
class UserInfo:
# 속성, 메소드
def __init__(self, name, height, weight, sex): # 클래스를 초기화 할떄 호출되는 함수 임 구현해주어야함
self.name = name # 셀프라는 유저의 인포의 인스턴스변수 안에 네임을 넣어준다.
self.height = height
self.weight = weight
self.sex = sex
def print_user_info(self):
print("name : ", self.name)
print("height : ", self.height)
print("weight : ", self.weight)
print("sex : ", self.sex)
user1 = UserInfo("kim", 175, 70, "male") # 클래스가 user1에 할당되는 이순간 init이 호출됌.
user2 = UserInfo("lee", 175, 49, "female")
user1.print_user_info()
user2.print_user_info()
print(id(user1), id(user2))
# 클래스를 이용해서 인스턴스화 해서 사용하고 있는데 인스턴스가 된 변수들은 서로 독립적인 네임스페이스라는 창고를 이용해서 요소들을 저장하고 있다.
print(user1.__dict__) # 네임스페이스 출력
print(user2.__dict__)
# 클래스 , 인스턴스 차이 중요
# 클래스 형태로 코딩을 해놓고 변수의 할당에서 인스턴스화 시켜서 클래스를 객체라고 하고 인스턴스화 시켜서 메모리에 올려서 페이로드로해서 사용한다
# 네임스페이스 : 객체를 인스턴스화 할 때 저장된 공간
# 클래스 변수 : 직접 사용 가능, 객체보다 먼저 생성
# 인스턴스 변수 : 객체마다 별도로 존재
# self의 이해
class SelfTest():
def function1():
print("function 1 called")
def function2(self):
print(id(self))
print("function 2 called")
self_test = SelfTest()
# self_test.function1()
SelfTest.function1()
self_test.function2()
# def function1() 은 클래스 메소드, def function2(self) 는 인스턴스 메소드
# 인스턴스를 생성을해야, self 안에 ,네임스페이스 안에 이름을 집어넣어놨으니까 2번펑션은 호출이 되는거고
# 1번함수는 셀프인자가 없어서 호출이 불가능한거임
print(id(self_test))
SelfTest.function2(self_test)
# 정리: 셀프가 들어간 함수는 인스턴스 함수, 없으면 클래스에서 직접호출할수 있는 공유함수
# 클래스 함수는 호출할떄 클래스이름으로 호출해야하고
# 나머지 셀프가 있는 함수는 인스턴스를 생성해서 호출하거나, 클래스를 바로 호출해서 인자로 넣어줘야한다.
# 셀프가 들어간건 인스턴스함수고 셀프가 없으면 클래스에서 직접호출할수있는 공통함수.
# 여러 인스턴스들이 공유하는 함수들이라고 볼수있고.
# 호출할떈 클래스 이름으로 호출해야하고 , 셀프가 있는건 인스턴스를 생성해서 호출하거나
# 인자를 넣어 클래스를 호출하던가.
# 예제3
# 클래스변수, 인스턴스 변수 (self 필요)
class Palette:
# 클래스 변수
color_num = 0 # 셀프가 없으니까 여러 인스턴스에서 공유가능
def __init__(self, name):
self.name = name
Palette.color_num += 1 # 클래스변수는 셀프가없으니까 바로 직접 접근해야함.
def __del__(self): # del은 인스턴스가 종료될떄 호출되는함수
Palette.color_num -= 1
color1 = Palette("Blue")
color2 = Palette("Red")
color3 = Palette("Yellow")
print(color1.__dict__)
print(color2.__dict__)
print(color3.__dict__)
print(Palette.__dict__) # 클래스 네임 스페이스, 클래스변수(공유)
print(color1.name)
print(color2.name)
print(color3.name)
print(color1.color_num)
print(color2.color_num)
print(color3.color_num)
del color1
print(color2.color_num)
print(color3.color_num)
>>>answer
name : kim
height : 175
weight : 70
sex : male
name : lee
height : 175
weight : 49
sex : female
4462324256 4462324368
{'name': 'kim', 'height': 175, 'weight': 70, 'sex': 'male'}
{'name': 'lee', 'height': 175, 'weight': 49, 'sex': 'female'}
function 1 called
4462324536
function 2 called
4462324536
4462324536
function 2 called
{'name': 'Blue'}
{'name': 'Red'}
{'name': 'Yellow'}
{'__module__': '__main__', 'color_num': 3, '__init__': <function Palette.__init__ at 0x109fa2488>, '__del__': <function Palette.__del__ at 0x109fa2510>, '__dict__': <attribute '__dict__' of 'Palette' objects>, '__weakref__': <attribute '__weakref__' of 'Palette' objects>, '__doc__': None}
Blue
Red
Yellow
3
3
3
2
2