logo
Search검색어를 포함하는 게시물들이 최신순으로 표시됩니다.
    Table of Contents
    [Python] 패키지(Package)

    이미지 보기

    [Python] 패키지(Package)

    • 21.07.20 작성

    • 읽는 데 2

    TOC

    패키지(Package)

    • 모듈들을 모아놓은 집합

    • 하나에 디렉토리에 여러 모듈 파일들을 모아놓은 것

    • package 명 : travel

    • module 명 : thailand, vietnam

    가. module 설정

    * thailand.py
    
    class ThailandPackage:
        def detail(self):
            print("[태국 패키지 3박 5일] 방콕, 파타야 여행 (야시장 투어) 50만원")
    
    
    * vietnam.py
    
    class VietnamPackage:
        def detail(self):
            print("[베트남 패키지 3박 5일] 다낭 효도 여행 60만원")
    

    나. package의 사용

    • travel package 내 thailand.py의 ThailandPackage class의 detail() 함수 사용
    • ※ import를 쓸 때 맨 뒤에는 모듈이나 패키지만 가능 / 클래스나 함수는 바로 import 불가능
    import travel.thailand
    trip_to = travel.thailand.ThailandPackage()
    trip_to.detail()
    

    출력값은 다음과 같다.

    [태국 패키지 3박 5일] 방콕, 파타야 여행 (야시장 투어) 50만원
    

    다. 함수/클래스의 호출

    • from ~~ 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()
    

    Reference

    profile

    FE Developer 박승훈

    노력하는 자는 즐기는 자를 이길 수 없다