溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python中模塊化編程的作用有哪些

發(fā)布時間:2021-01-18 15:13:30 來源:億速云 閱讀:290 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)Python中模塊化編程的作用有哪些,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

我們首先以一個例子來介紹模塊化編程的應(yīng)用場景,有這樣一個名為requirements.py的python3文件,其中兩個函數(shù)的作用是分別以不同的順序來打印一個字符串:

# requirements.py
def example1():
  a = 'hello world!'
  print (a)
  print (a[::-1])

def example2():
  b = 'hello again!'
  print (b)
  print (b[::-1])

if __name__ == '__main__':
  example1()
  example2()

其執(zhí)行結(jié)果如下所示:

[dechin@dechin-manjaro decorator]$ python3 requirements.py 
hello world!
!dlrow olleh
hello again!
!niaga olleh

在兩個函數(shù)中都使用到了同樣的打印功能,這時候我們可以考慮,是不是可以將這兩個打印語句封裝為一個函數(shù)呢,這樣不就可以重復(fù)利用了?這就是模塊化編程思維的雛形,讓我們先對樣例代碼進(jìn)行模塊化的改造:

# requirements.py
def rprint(para):
  print (para)
  print (para[::-1])

def example1():
  a = 'hello world!'
  rprint(a)

def example2():
  b = 'hello again!'
  rprint (b)

if __name__ == '__main__':
  example1()
  example2()

這里我們將兩個打印語句的功能實現(xiàn)封裝進(jìn)了rprint的函數(shù),執(zhí)行結(jié)果如下:

[dechin@dechin-manjaro decorator]$ python3 requirements.py 
hello world!
!dlrow olleh
hello again!
!niaga olleh

結(jié)果當(dāng)然還是與模塊化之前一致的。

向下封裝與向上封裝

在上一章節(jié)中,我們討論了python中的模塊化編程。由于在編程過程中有可能有大量的代碼需要復(fù)用,這時候就需要用一個函數(shù)來進(jìn)行封裝,來避免大量重復(fù)的工作。但是如果細(xì)分來看,這種封裝模式只解決了一類的問題:向下封裝。讓我們再看一次上述改進(jìn)后樣例中的代碼結(jié)構(gòu):

.
├── example1
│   └── rprint
└── example2
  └── rprint

我們可以發(fā)現(xiàn),這里復(fù)用的rprint實際上屬于兩個example函數(shù)的下層,我們可以稱之為向下封裝了一個rprint函數(shù)。那么,如果我們轉(zhuǎn)換一下需要復(fù)用的模塊,變成如下的代碼結(jié)構(gòu),那我們又需要用什么樣的方式來實現(xiàn)呢?

.
├── example
│  └── rprint1
└── example
  └── rprint2

問題解讀:該代碼結(jié)構(gòu)表示的意義為,有一個大的example函數(shù),該函數(shù)內(nèi)部嵌套不同的rprint函數(shù)可以實現(xiàn)不同的功能。為了方便理解,讀者可以想象成是有兩個函數(shù)example1和example2,這兩個函數(shù)中除了rprint1和rprint2這兩個函數(shù)模塊不一致以外,其他的部分都是完全一樣的,也就是可共用的。

Python的嵌套函數(shù)與裝飾器

首先,我們?yōu)榱藦?fù)盤上述章節(jié)中的問題,來構(gòu)造這樣的一個python測試代碼:

# requirements.py
def example1():
  def rprint1(para):
    print (para)
  a = 'hello world!'
  rprint1(a)

def example2():
  def rprint2(para):
    print (para[::-1])
  a = 'hello world!'
  rprint2(a)

if __name__ == '__main__':
  example1()
  example2()

以上代碼的執(zhí)行結(jié)果為:

[dechin@dechin-manjaro decorator]$ python3 requirements.py 
hello world!
!dlrow olleh

這個案例用到了python中嵌套函數(shù)的用法,在函數(shù)中可以嵌套實現(xiàn)另外的函數(shù)。這里我們注意到,雖然為了在同一個代碼串中嫩夠運行,兩個example函數(shù)的名字取的不同,但是實際上內(nèi)容是完全相同的,符合上一章節(jié)中遺留問題的代碼結(jié)構(gòu)。這里我們需要考慮的問題是,我們能否做到向上封裝,將example的同樣功能的代碼實現(xiàn)進(jìn)行歸類?那么我們需要引入裝飾器的用法,這里我們直接展示如何構(gòu)造修飾器,以及修飾器使用的效果。

# decorator.py
def example(func):
  def wrapper(*args, **kwargs):
    a = 'hello world!'
    return func(a)
  return wrapper

@example
def rprint1(para):
  print (para)

@example
def rprint2(para):
  print (para[::-1])

if __name__ == '__main__':
  rprint1()
  rprint2()

這個代碼的執(zhí)行結(jié)果為:

[dechin@dechin-manjaro decorator]$ python3 decorator.py 
hello world!
!dlrow olleh

從結(jié)果上我們就可以看到,這個代碼是實現(xiàn)了一樣的效果。通過example這個裝飾器,不僅封裝了上層函數(shù)中所實現(xiàn)的功能,而且還有一個重大意義是,通過裝飾器向下層函數(shù)傳遞了參數(shù)。這就使得,我們最終調(diào)用rprint函數(shù)的時候,不需要傳入任何的參數(shù),因為在example內(nèi)已經(jīng)定義了可以共享的參數(shù)。

看完上述內(nèi)容,你們對Python中模塊化編程的作用有哪些有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI