210802

nooyji·2021년 8월 2일
0

매직메소드

__init__

# 네임스페이스, 인스턴스의 저장공간
__dict__

id() # 메모리 주소값 return

클래스 메소드, 인스턴스 메소드

Class SelfTest:

	def function1(): # 클래스 메소드
    	print('function1 called!')
        
	def function2(self): # 인스턴스 메소드
    	print('function2 called!')
        
self_test = SelfTest()
SelfTest.function1()
self_test.function2()

클래스 변수 : 직접 사용가능, 객체보다 먼저 생성
인스턴스 변수 : 객체마다 별도 존재, 인스턴스 생성 후 사용
self 인자 = 인스턴스

Class WareHouse:
	stock_num = 0
    
    def __init__(self, name):
    	self.name = name
        WareHouse.stock_num += 1
        
    def __del__(self):
    	WareHouse.stock_num -= 1

자신의 namespace에 존재하지 않으면, 클래스 네임스페이스에 가서 찾는다 & 값을 가지고 온다

del user1 -> 인스턴스 삭제 + del 매직메소드 실행

Class BmwCar(Car): # BmwCar : 자식메소드, Car : 부모메소드

	def __init__(self, car_name, tp, color):
    	super().__init__(tp, color) # 부모메소드 call
        self.car_name = car_name
        
    def show_model(self) -> None:
    	return "Your Car Name : %s" %self.car_name

메소드 오버라이딩 -> 자식메소드에서 덮어씌우는거

부모메소드를 바로 Call -> super().부모메소드()

Inheritance Info
mro() : 상속 정보를 list 형태로 보여주는 + 상속받은 클래스 다 보여주는거

0개의 댓글