헬린코린이

[Python] 8일차 본문

Programming/Python

[Python] 8일차

HCD 2023. 5. 3. 16:10
#모듈 - Chapter
# 필요한 것들끼리 묶음끼리 잘 만들어진 파일이라고 보면 된다.
# 자동차로 예를 들면 타이어가 마모가 되면 타이어만 교체하면 된다
# 이런식으로 타이어처럼 부품만 갈면 유지보수도 쉽고 재사용성도 수월해지는 장점이있다.
# 확장자가 .py이다.

# 모듈은 모듈을 쓰려는 파일과 같은 경로와 있거나
# 파이썬 라이브러리들이 모여있는 폴더에 있어야 사용가능
import theater_module
theater_module.price(3) # 3명이서 영화 보러 갔을 떄 가격
theater_module.price_morning(4) # 4명이서 조조 할인 영화 보러 갔을 떄
theater_module.price_soldier(5) # 5명의 군인이 영화 보러 갔을 떄

import theater_module as mv #파일이 길떄 별명을 붙여서 줄일 수 있다.
mv.price(3)
mv.price_morning(4)
mv.price_soldier(5)

# 앞에 모듈에 있는 것을 사용하겠따.
from theater_module import *
# from random import *하고 같은 것
price(3)
price_morning(4)
price_soldier(5)

# 쓰고 싶은 메서드만 가져다가 쓸 수 있따. 
# 없는 메서드는 쓸 수  없다.
from theater_module import price_morning, price
price(5)
price_morning(6)

#별명으로 쓸 수 있다.
from theater_module import price_soldier as price
price(5) #솔져의 값이 출려된다


# 패키지 - Chapter
# 모듈들을 모아놓은 집합이라고 보면 됨
# 신규 여행사 프로젝트를 담당하겠다고 가정
# 이여행사는 태국 베트남 패키지 상품 제공
# 이 패키지 내용을 파이썬 내용으로 만들어서 누구나 사용할 수 있도록 함
import travle.thailand # 모듈이나 패키지만 가능함 클래스나 메서드 안됨!
trip_to = travle.thailand.ThailandPackage()
trip_to.detail()

# from import구문에서는 사용가능함
# 트라벨 패키지 안에 있는 모듈에서 타일랜드 패키지라는 클래스를 임포트 해온 것
from travle.thailand import ThailandPackage
trip_to = ThailandPackage()
trip_to.detail()

from travle import vietnam
trip_to = vietnam.VietnamPackage()
trip_to.detail()


# __all__ - Chapter
# 공개 범위를 설정할 수 있다. __init__.py에 공개 범위 설정을 해줘야한다.
from travle import *
print("dudd")
trip_to = vietnam.VietnamPackage()
trip_to = thailand.ThailandPackage()
trip_to.detail()

# 패키지 , 모듈 위치
# 패키지 위치 확인
import inspect
import random
print(inspect.getfile(random))
print(inspect.getfile(thailand))

패키지 위치

 

 

 

자료 : https://www.youtube.com/watch?v=kWiCuklohdY&t=1574s

Comments