import report as wr description = wr.get_description() print("Today's weather:", description)
def func_3_1(): print('--- 5.3.1 ---') # ここでmoduleの部分は、ほかのPythonファイルのファイル名から拡張子の.pyを取り除いたものである。 # report.pyは同じディレクトリに置かれる必要がある。 import report # reportとのプレフックスを使わなければならない。(名前の衝突が避けられる) description = report.get_description() print("Today's weather:", description)
def func_3_2(): print('--- 5.3.2 ---') ''' 同じ名前の別のモジュールが必要な時や、 もっと覚えやすい名前や簡潔な名前を使いたい場合、別名を使ってインポートする ''' import report as wr description = wr.get_description() print("Today's weather:", description)
def func_3_3(): print('--- 5.3.3 ---') ''' モジュールから一つ以上の部品だけをインポートすることができる。 もとの名前にするか別名を使うかは部品ごとに決められる。 ''' # もとの名前でインポートする from report import get_description print("Today's weather:", get_description()) # 別名(do_it)でインポートする from report import get_description as do_it print("Today's weather:", do_it())
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Jun 17 10:46:49 2017 @author: changyueh """ #指令列引數 import sys print('Program arguments:', sys.argv) #匯入模組 import report description = report.get_description() print("Today's weahter:", description) #模組搜尋路徑 page 121 #套件 page 121 weather.py / sources => daily.py weekly.py """ 實用的標準程式庫 page123 #模組文件http://docs.python.org/3/library #教學課程http://bit.ly/library-tour #Python Module of the Week http://bit.ly/py-motw #The Python Standard Library by Example http://bit.ly/py-libex """ #用setdefault()/defaultdict()來處理遺漏的鍵 page 123 periodic_table = {'Hydrogen': 1, 'Helium': 2} periodic_table.get('Oxgen') #返回值 carban = periodic_table.setdefault('Carban', 12) #鍵如果沒有,就使用新值
def weatherman(): description = report.get_description() print("today's weather:", description)
# import report as wr # description = wr.get_description() # print("Today's weather:", description) from report import get_description description = get_description() print("Today's weather:", description)
import report description = report.get_description() print("Today's weather:", description)
def get_description(): import random possibilities = ['rain', 'snow', 'sleet', 'fog', 'sun', 'who knows'] return random.choice(possibilities) import random def get_description(): possibilities = ['rain', 'snow', 'sleet', 'fog', 'sun', 'who knows'] return random.choice(possibilities) get_description() import report as wr description = wr.get_description() print("Today's weather:", description) from report import get_description description = get_description() print("Today's weather:", description) from report import get_description as do_it description = do_it() print("Today's weather:", description)
#chapter4 #Standalone Programs print(" this standalone program works!" ) #name it test1.py and type python tes1.py to see result #command-line arguments import sys print('program arguments: ', sys.argv) #modules and the import statement #import a module import report #report is another script named report.py description = report.get_description() print("today's weather: ", description) #import a module with another name import report as wr description = wr.get_description() print("tomorrow's weather: ", description) #Import Only What You Want from a Module from report import get_description description = get_description() print("tonight's weather: ", description) #same thing with another name from report import get_description as do_it description = do_it() print("morning's weather: ", description) #module search path import sys for place in sys.path: print(place) #packages from sources import daily, weekly #daily,weekly located in /sources print("daily forecast: ", daily.forecast())
from report import get_description description = get_description() print("Today's weather:", description)
from report import color from report import get_description print(get_description()) def sayHello(): print("sayHello")
import report import report as wr description = report.get_description() description2 = wr.get_description() print("Today's weather :", description) print("Today's weather2:", description2)