python--模組

來源:互聯網
上載者:User

標籤:uil   __file__   模組名   color   loader   pack   標準   參考   ann   

引入模組

文法

import modeName

 

one.py

def printSomething(str):    print("hello " + str);

 

main.py

import oneone.printSomething("jiao");     #hello jiaoone.printSomething("fftu");     #hello fftu

 

 

 

部分引入

文法

from modeName import functionName, varName

 

one.py

def printSomething(str):    print("hello " + str);str = "you should go now";def printbye():    print(str);

 

main.py

from one import printSomething, printbyeprintSomething("jiao");     #hello jiaoprintbye();     #you should go now

 

注意:部分引入不能使用模組名one

除了函數外,也可以引入模組的變數(不引入訪問報錯,但模組函數中是可以訪問這個變數的)

 

 

全部引入(將模組內所有函數、變數都引入當前檔案)

文法

from one import *

 

引入後用法和部分引入一樣

 

 

dir()函數

以一個字串列表的形式返回模組內定義的所有名稱

import oneprint(dir(one));    
#[‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘printSomething‘, ‘printbye‘, ‘str1‘]

 

沒有傳參返回當前檔案定義的所有名稱(引入的模組不做解析,只有一個名稱)

import onenumA = 10;strB = "haha";print(dir());#[‘__annotations__‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘numA‘, ‘one‘, ‘strB‘]

 

這樣會解析引入模組中的對象

from one import printSomething, printbye, str1numA = 10;strB = "haha";print(dir());#[‘__annotations__‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘numA‘, ‘printSomething‘, ‘printbye‘, ‘str1‘, ‘strB‘]

 

 

標準模組
Python 本身帶著一些標準的模組庫,在 Python 庫參考文檔中將會介紹到(就是後面的"庫參考文檔")。
有些模組直接被構建在解析器裡,這些雖然不是一些語言內建的功能,但是他卻能很高效的使用,甚至是系統級調用也沒問題。
這些組件會根據不同的作業系統進行不同形式的配置,比如 winreg 這個模組就只會提供給 Windows 系統。

 

python--模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.