Python3-筆記-C-007-函數-匯入模組並調用

來源:互聯網
上載者:User

標籤:port   on()   import   避免   module   size   turn   使用   函數   

# === 調用自訂模組 ===
#coding=utf-8
# mymodule.py 自訂模組
def myfunction():
return "myFunction"
# 避免外界調用函數時運行了測試代碼
if __name__ == "__main__":
print(myfunction())
==================================
# 匯入模組並調用 (三種匯入模組方式,選擇其中一種)
import time # 第一種, 引入單個模組
import time as t # 引入並重新命名
import time, calendar # 第二種, 引入多個模組
from mymodule import myfunction # 第三種, 匯入模組指定函數(*引入所有)
print(myfunction())


# === 動態匯入模組 ===
import importlib
modulename = "os"

# 方式1
obj = __import__(modulename) # 不推薦
# 方式2
obj = importlib.import_module(modulename) # 推薦使用
print(obj.getcwd())

Python3-筆記-C-007-函數-匯入模組並調用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.