Python_包
包是一个分层次的文件目录结构,由模块、子包等组成。包就是文件夹,但该文件夹下必须存在 __init__.py 文件, 该文件的内容可以为空。__init__.py 用于标识当前文件夹是一个包。
目录结构如下图所示:
# test.py from source import daily, weekly print("Daily forecast: ", daily.forecast()) print("Weekly forecast: ") for number, outlook in enumerate(weekly.forecast(), 1): print(number, outlook)
# daily.py def forecast(): mystring = "like yesterday" return mystring
# weekly.py def forecast(): return ['snow', 'more snow', 'sleet', 'freezing rain', 'rain', 'fog', 'hail']