Python 從模組匯入
從模組匯入
您可以透過使用 from
關鍵字來選擇僅從模組中匯入部分內容。
示例
名為 mymodule
的模組包含一個函式和一個字典。
def greeting(name)
print("Hello, " + name)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
注意: 使用 from
關鍵字匯入時,引用模組中的元素時不使用模組名。例如:person1["age"]
,而不是
。mymodule.person1["age"]