- 추상 함수
'''
추상 함수 : 자손 클래스에서 오버라이딩을 반드시 해야하는, 강제화 시키는 함수
함수의 구현부에 raise NotImplementedError를 기술함
'''
📌
class Parent :
def method(self): # 추상 함수
raise NotImplementedError
class Child(Parent) :
# pass # 결과 -> NotImplementedError. why? 반드시 오버라이딩 해야 하므로.
def method(self) : # 추상함수 오버라이딩
print("자손클래스에서 오버라이딩 함")
ch = Child()
ch.method()
- 모듈
📌 mod1.py 모듈예제

# -*- coding: utf-8 -*-
"""
Created on Tue Nov 29 10:33:31 2022
@author: KITCOOP
mod1.py 모듈예제
"""
def add(a,b) :
print("mod1")
return a+b
def sub(a,b) :
print("mod1")
return a-b
📌 mod2.py 모듈예제

# -*- coding: utf-8 -*-
"""
Created on Tue Nov 29 10:36:56 2022
@author: KITCOOP
mod2.py 모듈예제
"""
def add(a,b) :
print("mod2")
return a+b
def sub(a,b) :
print("mod2")
return a-b
print("mod2.py 실행함")
print("add(3,4)",add(3,4))
print("sub(3,4)",add(4,2))
📌 mod2.py 변경 전

# 모듈 : import 모듈명
# mod1.py, mod2.py 함수를 호출하기
import mod1 # mod1.py 파일의 내용 가져옴
import mod2 # mod2.py 파일의 내용을 가져옴
print("mod1 모듈 add()=",mod1.add(40,30))
print("mod1 모듈 sub()=",mod1.sub(40,30))
print("mod2 모듈 add()=",mod2.add(40,30))
print("mod2 모듈 sub()=",mod2.sub(40,30))
📌 mod2.py 변경 후

# -*- coding: utf-8 -*-
"""
Created on Tue Nov 29 10:36:56 2022
@author: KITCOOP
mod2.py 모듈예제
"""
def add(a,b) :
print("mod2")
return a+b
def sub(a,b) :
print("mod2")
return a-b
# mod2.py 직접 실행 하는 경우 __name__ 변수의 값이 __main__이다.
# mod2.py 직접 실행 하는 경우만 실행하도록 설정.
# import 되는 경우는 실행되지 않도록 설정
if __name__ == '__main__' : # 프로그램의 시작으로 인식
print("mod2.py 실행함")
print("add(3,4)=",add(3,4))
print("sub(3,4)=",sub(4,2))
📌
# 별명 설정 가능
import mod1 as m1 # mod1 모듈의 별명 설정
import mod2 as m2 # mod2 모듈의 별명 설정
print("mod1 모듈 add()=",m1.add(40,30))
print("mod1 모듈 sub()=",m1.sub(40,30))
print("mod2 모듈 add()=",m2.add(40,30))
print("mod2 모듈 sub()=",m2.sub(40,30))
# 일부분만 import
# import 되는 다른 모듈의 함수 이름이 같은 경우 주의가 필요
# 어느 모듈의 함수인지 판단이 필요하다.
from mod1 import add,sub
from mod2 import add
print("mod1 모듈 add()=", add(40,30))
print("mod1 모듈 sub()=", sub(40,30))
print("mod2 모듈 add()=", add(40,30))
print("mod2 모듈 sub()=", sub(40,30))
import mod1
import mod2
# dir(mod1) : mod1 모듈의 기본으로 제공되는 변수(내장변수)를 조회할 수 있는 함수
print("mod1의 기본 변수 :",dir(mod1)) # 기본 변수 조회
print("mod2의 기본 변수 :",dir(mod2))
print("__name__ :", __name__) # 현재 파일 __name__ 변수 출력. 결과 -> __main__
print("mod1.__name__ :", mod1.__name__) # 시스템에서 제공해 주는 변수
print("mod2.__name__ :", mod2.__name__) # 결과 -> mod2
'수업(국비지원) > Python' 카테고리의 다른 글
| [Python] 파일 정보 조회 (0) | 2023.04.24 |
|---|---|
| [Python] 파일 읽기 (0) | 2023.04.24 |
| [Python] 클래스에서 사용되는 특별한 함수들 (0) | 2023.04.24 |
| [Python] 멤버 변수 - 인스턴스 변수, 클래스변수 와 상속 (0) | 2023.04.24 |
| [Python] 클래스 (0) | 2023.04.24 |