Pyhon學(xué)習(xí)筆記3:模組(引用第三方模塊)

模組更通俗的講叫類(lèi)庫(kù)或者模塊,在實(shí)際開(kāi)發(fā)中,會(huì)使用到系統(tǒng)的標(biāo)準(zhǔn)模塊或者第三方模塊。例如,與時(shí)間有關(guān)的是系統(tǒng)的time塊,文件操作相關(guān)的是os模塊,使用selenium實(shí)現(xiàn)的web自動(dòng)化測(cè)試,使用的是selenium擴(kuò)展塊。

創(chuàng)新互聯(lián)技術(shù)團(tuán)隊(duì)10多年來(lái)致力于為客戶提供網(wǎng)站建設(shè)、做網(wǎng)站、成都品牌網(wǎng)站建設(shè)、營(yíng)銷(xiāo)型網(wǎng)站、搜索引擎SEO優(yōu)化等服務(wù)。經(jīng)過(guò)多年發(fā)展,公司擁有經(jīng)驗(yàn)豐富的技術(shù)團(tuán)隊(duì),先后服務(wù)、推廣了上千網(wǎng)站,包括各類(lèi)中小企業(yè)、企事單位、高校等機(jī)構(gòu)單位。

  1. 引用模塊:
    import  ...

  from .. import...

  舉例:輸出系統(tǒng)時(shí)間

 

import time

print(time.ctime())

結(jié)果:

======================= RESTART: D:/selenium/test/5.py =======================

Thu Nov  3 15:08:13 2016

>>> 

如果確定只引入time模塊下的ctime方法,則可以直接使用 from time import ctime

from time import *

print(ctime())

sleep(2)

print(ctime())

輸出結(jié)果:

======================= RESTART: D:/selenium/test/5.py =======================

Thu Nov  3 15:12:57 2016

Thu Nov  3 15:12:59 2016

>>> 

2模塊間的調(diào)用

新建一個(gè)模塊,在該模塊下創(chuàng)建兩個(gè)文件pub.py華為count.py

文件 pub.py:

def add(a,b):

    return a+b

文件count.py:

from pub import add

print(add(4,5))

打印結(jié)果:

===================== RESTART: D:/selenium/test/count.py =====================

9

>>> 

3.跨目錄模塊調(diào)用

文件模塊目錄:

project/

|--model/

  |--pub.py

|--count.py

cout.py代碼:

from model.pub import add

print(add(4,5))

4.進(jìn)一步討論跨目錄調(diào)用

文件目錄:

project/

|--model/

  |--count.py

  |--new_count.py

|--test.py

count.py文件代碼:

class A():

    def add(self,a,b):

       return a+b

new_count.py文件代碼

from count import A

class B(A):

    def sub(self,a,b):

        return a-b

st =B().add(2,5)

print(st)

test.py文件代碼:

import sys

sys.path.append("./model")

fome model import new_count

test = new_count.B()

test.add(2,5)


分享名稱(chēng):Pyhon學(xué)習(xí)筆記3:模組(引用第三方模塊)
瀏覽地址:http://www.muchs.cn/article34/gjggse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、手機(jī)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、微信公眾號(hào)、全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)