Python/문법정리
[모듈] 파일 생성 & import
Fullyalive
2017. 8. 13. 11:35
모듈?
변수, 함수, 클래스 등을 모아 놓은 파일. 모듈에 함수나 정보등을 정의해두고 가져다 쓰는 식
calculator.py 파일 생성
#합
def sum(x, y):
return x + y
# 차이
def difference(x, y):
return x + y
# 곱
def product(x, y):
return x * y
# 제곱
def square(x):
return x * x
이후 이 모듈을 쓸 파이썬 문서에서
from calculaotr import sum, square #difference, product도 가능, from calculator import * < 전부 임포트할 경우
print(sum(3, 5)) # 이런식으로 불러 쓸 수 있다.