공부하는게 취미인 사람들 투딩
# 모듈 페이지 만들기
# 파일명 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))
#모듈 호출 페이지 : practice.py
#모듈은 같은 경로에 있거나 파이썬 라이브러리들이 모여 있는 폴더에 같이 있어야 한다.
#사용법 1
코드
import theater_module #.py를 제외한 모듈파일명
theater_module.price(3) #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 : theater_module 의 모든 것을 쓰겠다. xxx. 없이 바로 사용가능
코드
from theater_module import *
price(3)
price_morning(4)
price_soldier(5)
#사용법 4 : #필요함수만 명시적으로 사용 가능
코드
from theater_module import price,price_morning
price(3)
price_morning(4)
#사용법 5 : 필요한 함수에 별명을 붙여 쓸수 있다
코드
from theater_module import price_soldier as price
price(5) ①
① 모듈에 정의된 price 가 아니라 별명으로 정의된 price. 값은 모듈에서 정의된 price_soldier 타고 나온다.
패키지 : 모듈을 모아놓은 집합
# 패키지 폴더/페이지 만들기
폴더명 : travel / 페이지명 : __init__.py , thailand.py , vietname.py
# travel / thailand.py
코드
class ThailandPackage:
def detail(self):
print("[태국 패키지 3박 5일] 방콕, 파타야 여행 (야시장 투어) 50만원")
# travel / vietname.py
코드
class VietnamPackage:
def detail(self):
print("[베트남 패키지 3박 5일] 다낭 효도 여행 60만원")
#practice_packuse.py → 패키지 사용 페이지
코드
import travel.thailand ①
trip_to = travel.thailand.ThailandPackage()
trip_to.detail()
① import 폴더명.파일명 모듈이나 패키지만 호출 가능, 클래스나 함수는 바로 import 할수 없다.
# from import 구문에서는 클래스명 호출 가능
코드
from travel.thailand import ThailandPackage
trip_to = ThailandPackage()
trip_to.detail()
# from 폴더명 import 파일명
코드
from travel import vietnam
trip_to = vietnam.VietnamPackage()
trip_to.detail()
# *를 써서 아래 처럼 하면 될것 같은데 에러...
from travel import *
trip_to_vietname = vietnam.VietnamPackage()
trip_to_vietname.detail()
trip_to_thailand = thailand.ThailandPackage()
trip_to_thailand.detail()
#사용 가능하게 하기 위해서 공개범위를 설정해줘야 * 를 쓸수 있다. __init__ 에서 처리 해줘야 함
# travel / __init__.py
코드
__all__ = ["vietnam" , "thailand"]
#잘 돌긴 하는데 찝찝하게 빨간줄이 가고 있음... 비주얼 스튜디오 코드에서 설정 변경
#__main__ 사용하기
# travel / thailand.py
코드
class ThailandPackage:
def detail(self):
print("[태국 패키지 3박 5일] 방콕, 파타야 여행 (야시장 투어) 50만원")
if __name__ == "__main__":
print("Thailand 모듈을 직접실행")
print("이 문장은 모듈을 직접 실행할 때만 실행되요")
trip_to = ThailandPackage()
trip_to.detail()
else:
print("Thailand 외부에서 모듈 호출")
# 코드 추가하면 thailand.py 에서 실행하면 if 안쪽 구문이 실행된다... main 별도 정의 없이 바로 저렇게 쓸수 있나봄.
#같은 폴더 위치의 모듈을 호출해서 쓸수 있다. 위치 확인 하기
#모듈 파일 위치 찾기
코드
from travel import *
import inspect
import random
print(inspect.getfile(random))
print(inspect.getfile(thailand))
#다른 프로젝트에서도 사용할수 있게
#pip install
파이썬 버전 정보 확인
python --version
설치
pip3 install beautifulsoup4
삭제
pip3 uninstall beautifulsoup4
설치된 pip 목록 보기
pip list
블로그에는 우클릭 방지가 걸려있어요. 코드복사는 카페에서 가능 합니다.
파이썬 기초는 나도코딩님이 인프런사이트에서 강의한 내용을 바탕으로 공부한 내용을 정리한 것 입니다.
추천 : ★★★★★
'코딩(개발) > 파이썬' 카테고리의 다른 글
파이썬 게임 만들기 (0) | 2020.12.24 |
---|---|
파이썬 내장함수 / 외장함수 (0) | 2020.12.23 |
파이썬 예외처리 (0) | 2020.12.22 |
파이썬 class (0) | 2020.12.21 |
파이썬 파일 입출력 (0) | 2020.12.20 |
댓글