모듈: 필요한 것들을 부품처럼 잘 만들어진 파일들 불러오겠다.(확장명 .py)
- 모듈 파일(theater_module.py)
#일반 가격
def price(people):
print("{0}명 가격은 {1}원 입니다.".format(people,people*10000))
#조조 가격
def price_morning(people):
print("{0}명 조조 할인 가격은 {1}원 입니다.".format(people,people*6000))
#군인 가격
def price_soldier(people):
print("{0}명 군인 할인 가격은 {1}원 입니다.".format(people,people*4000))
-모듈 파일 불러오기
1. 파일에서 불러오기 import 파일명
2. 파일에서 불러오는데 별명으로 쓰겠다 import 파일명 as 별명
3. 파일 전체 불러오므로 파일명 빼겠다 from 파일명 import*
4. 파일에서 필요한 함수만 가져오겠다 from 파일명 import 함수1, 함수2
5. 파일에서 필요한 함수만+ 그 함수 별명으로 쓰겠다 from 파일명 import 함수1 as 별명
import theater_module
theater_module.price(3) #3명이서 영화보러 갔을 때 가격
theater_module.price_morning(4)
theater_module.price_soldier(5)
import theater_module as mv
mv.price(3)
mv.price_morning(4)
mv.price_soldier(5)
from theater_module import* #theater_module에 있는거 가져오겠다는 뜻
#from random import * 이전에 배운 내용
price(3)
price_morning(4)
price_soldier(5)
from theater_module import price,price_morning #필요한 함수만 가져올 수도 있음
price(3)
price_morning(4)
price_soldier(7) #price와 price_morning만 가져온다고 했기에 이거는 오류남
from theater_module import price_soldier as ps
ps(5)
패키지(package): 모듈을 모아놓은 것
-thailand.py 모듈
class thailandpackage:
def detail(self):
print("[태국 패키지 3박 5일] 방콕,타파야 여행(야시장 투어) 50만원"
-vietnam.py 모듈
class vietnampackage:
def detail(self):
print("[베트남 패키지 3박 5일] 다낭 효도 여행 60만원")
- 새로운 창에서 import 하기
import travel.thailand #맨 뒤에 오는 것은 모듈이나 패키지만 가능. 클래스나 함수는 바로 import 불가
trip_to=travel.thailandpackage()
trip_to.detail()
from travel.thailand import thailandpackage
trip_to=thailandpackage()
trip_to.detail()
from travel import vietnam
trip_to=vietnam.vietnampackage()
trip_to.detail()
Terminal>>
[태국 패키지 3박 5일] 방콕,타파야 여행(야시장 투어) 50만원
[태국 패키지 3박 5일] 방콕,타파야 여행(야시장 투어) 50만원
[베트남 패키지 3박 5일] 다낭 효도 여행 60만원
__all__ : __init__.py 파일에서 정의한 것만 불러올 수 있게 함. 공개처리 한 것. 다른 것은 숨김처리.
-__init__.py 모듈
__all__=["vietnam"]
라고 입력하면
-새로운 창에서 import 할 때
from travel import * #출력됨. __all__에 적어놔서
from travel.vietnam import vietnampackage
trip_to= vietnampackage()
trip_to.detail()
from travel import* #출력안됨. __all__에 안적어놔서
from travel.thailand import thailandpackage
trip_to= thailandpackage()
trip_to.detail()
name(모듈 직접 실행) : 두 번째 문단
-thailand.py 파일에서 실행 (첫 번째, 두 번째 문단이 실행됨.)
class thailandpackage:
def detail(self):
print("[태국 패키지 3박 5일] 방콕,타파야 여행(야시장 투어) 50만원")
if __name__=="__main__": #thailand.py 파일에서는 이 문단이 실행될 것임
print("thailand 모듈을 직접 실행")
print("이 문장은 모듈을 직접 실행할 때만 실행돼요")
trip_to = thailandpackage() # trip_to라는 변수 만들고 tailandpackage라는 객체 생성
trip_to.detail()
else:
print("thailand 외부에서 모듈 호출")
#practice.py 파일에서 thailandpackage 갖다 쓸 때 실행되는 구문
1. class 하위에 name을 이용해서 if __name__ == "__main__" 이라면
2. if제어문 하위 두 print( )출력이 될 것임.
Terminal>>
thailand 모듈을 직접 실행
이 문장은 모듈을 직접 실행할 때만 실행돼요
[태국 패키지 3박 5일] 방콕,타파야 여행(야시장 투어) 50만원 << trip_to =thailandpackage(), trip_to.detail() 실행
-thailand.py 파일에서 실행 (세 번째 문단-캡쳐본-실행됨.)
from travel import*
from travel.thailand import thailandpackage
trip_to= thailandpackage()
trip_to.detail()
모듈 파일 내부가 아니라면(else) 이 문장을 출력해라.
thailand 외부에서 모듈 호출 << 모듈 파일에 있는 else: print("thailand 외부에서 모듈 호출")의 결과
[태국 패키지 3박 5일] 방콕,타파야 여행(야시장 투어) 50만원
'Python' 카테고리의 다른 글
[파이썬] 모듈 Ⅲ (내장함수 input, dir & 외장함수 glob, os, time, datetime, timedelta) (2) | 2024.11.11 |
---|---|
[파이썬] 모듈 Ⅱ (모듈 위치확인-inspect& getfile, 패키지 다운로드 및 활용- pypi, pip install ) (1) | 2024.11.10 |
[파이썬] 예외처리(try, except, ValueError, ZeroDivision, raise, __str__, break, finally) (0) | 2024.11.10 |
[파이썬] 클래스class Ⅲ ( pass, super ) (1) | 2024.11.09 |
[파이썬]클래스class Ⅱ (상속, 다중상속, 메소드오버라이딩) (0) | 2024.11.09 |