# else: # print("[대기번호 {0}] {1}마리 주문이 완료되었습니다.".format(waiting, order)) # waiting +=1 # chicken -=order # if chicken ==0: # raise SoldOutError # except ValueError: # print("잘못된 값을 입력하였습니다.") # except SoldOutError: # print("재고가 소진되어 더 이상 주문을 받지 않습니다.") # break #모듈 부품끼리, 만들어진 타이어만 교체, 부품만 교체하도록 (유지보수 쉽고 수정 쉽게) #잔돈을 돌려주지 않는 영화관, 금액에 맞춰서 줘야한다. (가격을 미리 알 수 있는 모듈 만들어보자.) 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 randeom import* price(3) price_morning(4) price_soldier(5) from theater_module import price, price_morning #필요한 것만
# 모듈 theater_module.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 * # price(3) # price_soldier(4) # price_morning(5) # from theater_module import price, price_morning # price(5) # price_morning(6) # price_soldier(7) from theater_module import price_soldier as price price(5)
# import theater_module # theater_module.price(3) # 3명이서 영화 보러 갔을 때 가격 # theater_module.price_mornig(4) # theater_module.price_soldier(5) # import theater_module as mv # theater_module가 이름이 너무 기니까 mv라고 지정해주는 것 # mv.price(3) # mv.price_mornig(4) # mv.price_soldier(5) # # theater_module을 붙히지 않고 그 안에 모든 라이브러리를 사용하겠다. # from theater_module import * # price(3) # price_mornig(4) # price_soldier(5) # #원하는 함수만 쓰도록 지정해줄 수 있다. # from theater_module import price, price_mornig # price(3) # price_mornig(4) # # price_soldier(5) # 가져올 함수를 또 as로 이름을 지정해줄 수 도 있다. from theater_module import price_soldier as price price(3) # 여기서 prcie 는 theater_module에 있는 price가 아닌 price_soldier다
print("프로그램을 종료합니다") # urllib # from urllib import request # target = request.urlopen("https://www.naver.com") # output = target.read() # print(output) # 극장에서 현금만 받는다. 잔돈을 바꿔주지않는다. 정확히 사람수에 따라 가격이 얼마인지 계산하는 모듈 # theater_module.py로 만들기 (현 프로젝트 폴더와 같은 경로에 만들기) import theater_module theater_module.price(3) # 3명 일반가격 theater_module.price_morning(4) # 조조할인 가격 theater_module.price_soldier(5) # 군인할인 가격 print() import theater_module as tm tm.price(3) tm.price_morning(4) tm.price_soldier(5) print() from theater_module import * price(3) price_morning(4) price_soldier(5) print()
### 모듈 import theater_module # 같은 폴더 안에 있어야지 사용가능 theater_module.price(3) theater_module.price_moring(4) import theater_module as mv mv.price(3) mv.price_moring(4) from theater_module import * price(3) price_moring(4) from theater_module import price_moring as price price(4) ### 패키지 import travel.thailand trip_to = travel.thailand.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() ### __all__(init에서 사용) from travel import *
# 일정 부분만 교체하고 추가할 수 있도록 만들어 코드의 재사용, 유지 보수를 쉽게 하는 것 # 파이썬 모듈 확장자: .py # import theater_module import theater_module as tm tm.price(3) tm.price_soldier(4) tm.price_morning(5) from theater_module import price, price_morning, price_soldier price(3) price_morning(4) price_soldier(5) from theater_module import price_soldier as ps ps(5)
''' 모듈 : 필요한 것들 끼리 잘 만들어진 파일 theater_module.py 사용 1. 모듈은 지금 사용하는 같은 파일에 위치하거나, 2. 파이썬 라이브러리들이 모여있는 파일에 위치해 있을 경우 사용 가능 ''' ''' # import theater_module # theater_module.price(3) # theater_module.price_morning(2) # theater_module.price_solider(4) # import theater_module as mv # 별명 사용 가능 # mv.price(3) # mv.price_solider(4) # mv.price_morning(1) from theater_module import * # from random import * # 모듈명이나 모듈 별병을 입력하지 않고 바로 모듈 내 함수를 사용할 수 있다. price(3) price_solider(5) price_morning(2) '''
'''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 * price(3) price_morning(4) price_soldier(5)''' '''from theater_module import price, price_morning # 필요한 함수만 가져다쓰기 price(5) price_morning(4)''' from theater_module import price_soldier as price price(5) # 군인할인가격
theater_module.price_mornig(4) # 4명이서 조조 영화 보러 갔을 때 가격 theater_module.price_soldier(5) # 군인 5명이서 영화 보러 갔을 때 가격 ''' ''' import theater_module as mv # theater_module을 mv로 별명을 붙여주어 모듈명을 줄여서 사용가능 mv.price(3) mv.price_mornig(4) mv.price_soldier(5) ''' ''' from theater_module import * # theater_module 필요없이 모든 것을 사용하겠다. price(3) price_mornig(4) price_soldier(5) ''' ''' from theater_module import price, price_mornig # 선택적으로 가져올 수 도 있다. price(3) price_mornig(4) # price_soldier(5) : 가져오지 않았기 때문에 오류남 '''
# 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, price_morning # 군인 함수는 쓰지 않고싶을 때 from theater_module import price_soldier as price price(5) # 군인 할인 가격
# 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 별명으로 호출 모듈명이 길 때 # 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, price_morning #모듈 내에서 필요한 것만 호출 # price(5) # price_morning(3) # #price_soldier는 오류가 난다. 호출을 안했기 때문에. from theater_module import price_soldier as price price(4)
### 모듈 import theater_module # 파일명만 입력하여 모듈 가져옴 theater_module.price(3) # 인원 3명의 일반가격/ 출력: 3명분 가격은 30000원 입니다. theater_module.priceMorning(4) # 인원 4명의 조조가격/ 출력: 4명분 조조 할인 가격은 24000원 입니다. theater_module.priceSoldier(5) # 인원 5명의 군인가격/ 출력: 5명분 군인 할인 가격은 20000원 입니다. import theater_module as mv # theater_module를 mv라는 별칭을 붙임 mv.price(3) # theater_module.price(3)과 같음 from theater_module import * # theater_module에 관한 모든 것을 사용 price(3) # 위와 같음 from theater_module import price, priceMorning # 특정 함수만 가져옴 price(5) priceMorning(6) # priceSoldier는 사용 불가 from theater_module import priceSoldier as price price(2) # 2명분 군인 할인 가격은 8000원 입니다. # theater_module의 price함수가 아님.
import theater_module as mv # theater_module를 mv란 변수명으로 정의 mv.price(3) # 3명 일반가격 mv.price_morning(4) # 4명 조조할인 mv.price_soldier(5) # 5명 군인할인 from theater_module import * price(3) price_morning(4) price_soldier(5) from theater_module import price, price_morning price(5) price_morning(6) # price_soldier를 import하지 않았기때문에 사용 불가 from theater_module import price_soldier as ps # theater_module의 price_soldier를 ps라 정의 ps(5)
# import theater_module # theater_module.price(3) # 3명이서 갔을 때 # theater_module.price_morning(3) # 3명이서 갔을 때 # theater_module.price_soldier(3) # 3명이서 갔을 때 # import theater_module as mv # mv.price(3) # 3명이서 갔을 때 # mv.price_morning(3) # 3명이서 갔을 때 # mv.price_soldier(3) # 3명이서 갔을 때 # from theater_module import * # price(3) # 3명이서 갔을 때 # price_morning(3) # 3명이서 갔을 때 # price_soldier(3) # 3명이서 갔을 때 from theater_module import price, price_morning price(3) # 3명이서 갔을 때 price_morning(3) # 3명이서 갔을 때 # price_soldier(3) # 3명이서 갔을 때 from theater_module import price_soldier as price price(3) # 군인 3명이서 갔을 때
''' import theater_module # theater_module.py 모듈에서 확장자명은 입력할 필요 없음 #모듈 사용방법1 : theater_module 모듈명 입력 후 뒤에 .price() 처럼 함수를 호출해서 사용 theater_module.price(3) # 3명이 영화 보러 갔을 때 가격 theater_module.price_mornig(4) # 4명이 조조 할인 영화 보러 갔을 때 가격 theater_module.price_soldier(5) # 3명의 군인이 영화 보러 갔을 때 가격 ''' ''' # 모듈 사용방법2 : 모듈 이름이 길 경우 as 를 이용해 별칭을 부여해 사용 import theater_module as mv mv.price(3) mv.price_mornig(4) mv.price_soldier(5) ''' ''' # 모듈 사용방법3 : from을 이용해 theater_module 글자 안 적고 바로 price() 함수를 호출해서사용 from theater_module import * # theater_module 의 내용을 모두 가져와 사용하겠다는 의미 price(3) price_mornig(4) price_soldier(5) ''' ''' # 모듈 사용방법4: 모듈에서 필요한 함수만 import 해서 사용하기 from theater_module import price, price_mornig #내가 사용할 함수만 명시적으로 입력해서 사용 price(3) price_mornig(4) # price_soldier(5) # 사용 불가(x) ''' # 모듈 사용방법5: 모듈에서 필요한 함수만 import한 이후 as를 이용해 별칭을 부여해서 사용하기
# 11-1. 모듈 (a.k.a. 부품, 확장자 - .py) # 모듈사용 - import import theater_module theater_module.price(3) # 3명이서 영화 보러 갔을 때 가격 theater_module.price_mornig(4) # 4명이서 조조 할인 영화 보러 갔을때 theater_module.price_soldier(5) # 5명의 군인이 영화 보러 갔을때 # as import theater_module as mv mv.price(3) mv.price_mornig(4) mv.price_soldier from theater_module import * # theater_module 모듈 모두 사용 # from random import * price(3) price_mornig(4) price_soldier(5) from theater_module import price, price_mornig, price_soldier price(5) price_mornig(6) price_soldier(7) # 오류 from theater_module import price_soldier as price price(5) # == price_soldier(5) # 11-2. 패키지 (모듈들을 모아놓은 집합) import travel.thailand
import theater_module theater_module.price(3)
# 방법 1 # import theater_module # theater_module.price(3) # 3명이서 영화 보러 갔을 때 가격 # theater_module.price_morning(4) # 4명이서 조조 할인 영화 보러 갔을 때 # theater_module.price_soldier(5) # 5명의 군인이 영화 보러 갔을 때 # 방법 2 # 모듈의 별칭을 주어 간단하게 활용하는 방법 # import theater_module as mv # mv.price(3) # mv.price_morning(4) # mv.price_soldier(5) # 방법 3 # from theater_module import * # theater_module의 모든(*)것을 사용하겠다. # price(3) # price_morning(4) # price_soldier(5) # 방법 4 : from ~ import의 변형 # from theater_module import price, price_morning # 필요한 것들만 골라서 import 할 수 있다. # price(8) # price_morning(10) # 방법 5 : 모듈의 1개의 기능만 import 하는 경우에도 별칭을 붙일 수 있다. from theater_module import price_soldier as price price(10)
## 11.1 모듈 ################################################################################ # 모듈: 잘 만들어진 파일 (예: 자동차의 부품들의 모임, 유지보수 코드의 재사용) # 모듈파일들은 모두 한 디렉토리에 존재해야만 사용가능 print(11.1) # 방법 1 import theater_module theater_module.price(3) theater_module.price_morning(4) theater_module.price_soldier(5) # 방법 2 import theater_module as mv mv.price(3) mv.price_morning(4) mv.price_soldier(5) # 방법 3 from theater_module import * # from random import * price(3) price_morning(4) price_soldier(5) # 방법 4 from theater_module import price, price_morning price(5) price_morning(6) # price_soldiert # Error # 방법 5 (방법 2와 4)
from theater_module import price_soldier as price from theater_module import price, price_morning from theater_module import * import theater_module as mv # 별명으로 이름 변경 import theater_module theater_module.price(3) # 3명이서 영화 보러 갔을 때 가격 theater_module.price_morning(4) theater_module.price_soldier(5) mv.price(3) mv.price_soldier(4) mv.price_soldier(5) price(3) price_morning(4) price_soldier(5) price(3) price_morning(5) price(5)
# import theater_module # theater_module.price(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 * # price(3) # price_morning(4) # price_soldier(5) # from theater_module import price, price_morning # price(3) # price_morning(4) from theater_module import price_soldier as price price(3)
# 방법1 import theater_module # .py 붙이지 않기 theater_module.price(4) theater_module.price_morning(3) theater_module.price_soldier(5) # theater_module 반복됨 # 방법2 import theater_module as tm # theater_module 반복을 피하기 위해 tm으로 이름붙임 tm.price(3) tm.price_morning(2) # 방법3 from theater_module import * # from random import * price(6) price_soldier(4) # 방법4 from theater_module import price, price_morning #theater_module이름의 모듈에서 price와 price_morning을 가져다 쓰겠다. price(7) # 방법5 from theater_module import price_soldier as cheap cheap(5) '''[import 모듈] → 해당 모듈 전체를 가져온다. 사용하려면 항상 '모듈명.메소드' 와 같이 모듈명을 앞에 붙여주어야 한다. [from 모듈 import 메소드 / 변수] → 해당 모듈 내에 있는 특정 메소드나 모듈 내 정의된 변수를 가져온다. 가져온 메소드나 변수를 앞에 모듈명을 붙이지 않고 그대로 사용할 수 있다. 다만, 이름이 같은 변수나 메소드가 존재할 경우 대체된다.
try: num1 = int(input("첫 번째 숫자를 입력하세요 : ")) num2 = int(input("첫 번째 숫자를 입력하세요 : ")) print("{0} / {1} = {2}".format(num1, num2, int(num1 / num2))) except ValueError: print("에러") except ZeroDivisionError as err: print(err) except Exception as err: #다른 모든 에러 print(err) # if, raise로 에러 커스텀 가능. import theater_module as mv mv.price(3) from theater_module import * price(3) #패키지는 모듈의 집합 import travel.thailand #방법1 trip_to = travel.thailand.ThailandPackage() trip_to.detail() from travel.thailand import ThailandPackage #방법 2 trip_to = ThailandPackage() trip_to.detail() from travel import vietnam trip_to = vietnam.VietnamPackage() trip_to.detail()
#import theater_module as mv '''mv.price(3) mv.price_morning(4) mv.price_soldier(5) ''' #from theater_module import * # import 내용물 전부 가져오기 import 후 가져올 부분만 가져올수잇음 # ex from random import * from theater_module import price_soldier as price price(3) # price_soldier 의 기능이 실행된다
''' import theater_module theater_module.price(3) theater_module.price_morning(1) theater_module.price_soldier(5) ''' import theater_module as mv #mv로 별명 바꾸기 mv.price(3) from theater_module import * price(3) price_morning(3) price_soldier(1) from theater_module import price_morning, price #except soldier price_morning(2) price(1)
''' 모듈 ''' # import theater_module # theater_module.price(3) # theater_module.price_morning(3) # theater_module.price_soldier(3) import theater_module as mv mv.price(2) mv.price_morning(3) mv.price_soldier(4) # from theater_module import * # # from random import * # price(3) # price_morning(4) # price_soldier(5) # from theater_module import price, price_morning # price(2) # price_morning(2)
if chicken == 0: raise SoldOutError except ValueError: print("잘못된 값을 입력하였습니다.") except SoldOutError: print("재고가 소진되어 더 이상 주문을 받지 않습니다.") break # 무한루프 탈출하기 # 모듈 # : 확장자 .py 필요한 것들끼리(함수 정의 클래스 정의) 담은 라이브러리 theater_module.price(3) theater_module.price_morning(4) theater_module.price_soldier(5) mv.price(2) mv.price_morning(4) mv.price_soldier(3) price(4) price_morning(2) price_soldier(4) price(5) price_morning(6)
# import theater_module # theater_module.price(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 * # price(3) # price_morning(4) # price_soldier(5) # from theater_module import price, price_morning # price(3) # price_morning(4) # price_soldier(5) # error from theater_module import price_soldier as price price(5) #price_soldier