superkind
1/4/2018 - 12:33 PM

모듈과 import #python #tutorial

모듈과 import #python #tutorial

모듈 임포트하기

test.py

import report

DESCRIPTION = report.get_description()
print("Today's weather:", DESCRIPTION)

report.py

def get_description():  # ㅂㅏ로 아래에 docstring
    """Return random weather, just like the pros"""
    from random import choice
    possibilities = ['rain', 'snow', 'sleet', 'fog', 'sun', 'who knows']
    return choice(possibilities)

다른 이름으로 모듈 임포트

import report as wr
DESCRIPTION = wr.get_description()

필요한 모듈만 임포트하기

from report import get_description
DESCRIPTION = get_description()
from report import get_description as do_it
DESCRIPTION = do_it()