99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀妙不可言!寫出優(yōu)雅的 Python 代碼的七條重要技巧
妙不可言!寫出優(yōu)雅的 Python 代碼的七條重要技巧
2021-11-26
收藏
妙不可言!寫出優(yōu)雅的 Python 代碼的七條重要技巧

作者:豆豆

來(lái)源:Python 技術(shù)

寫出能完成功能的程序每個(gè)程序員都可以搞定,但能寫出優(yōu)雅的程序的程序員卻寥寥無(wú)幾,因此程序?qū)懙膬?yōu)雅與否則是區(qū)分頂級(jí)程序員與一般程序員的終極指標(biāo)所在。

那身為一名 Pythoner,有哪些技巧能讓我們寫出優(yōu)雅的 Python 代碼呢,今天派森醬就給大家介紹七個(gè)能快速提升代碼逼格的重要技巧。

0x00 規(guī)范命名

沒有哪個(gè)程序員會(huì)抗拒一段命名規(guī)范的代碼!

命名作為編程界的一大難題,實(shí)屬難倒了很多人。不知道你是否還記得自己那些曾經(jīng)很沙雕的命名呢。

a,b,c  x,y,z a1,a2 4_s,4s... def do_something(): def fun(): ...

相信你看到上面的命名也是一頭霧水,好的命名不一定要寫的多優(yōu)雅,最起碼要做到見名識(shí)意。統(tǒng)一的命名風(fēng)格可以讓代碼看起來(lái)更簡(jiǎn)潔,風(fēng)格更統(tǒng)一,這樣閱讀者一看就知道這個(gè)變量或者函數(shù)是用來(lái)干嘛的,不至于猜半天浪費(fèi)過多的精力在不必要的事情上。

0x01 面向?qū)ο?

Python 是一門面向?qū)ο笳Z(yǔ)言,因此我們有必要熟悉面向?qū)ο蟮囊恍┰O(shè)計(jì)原則。

單一職責(zé)原則是指一個(gè)函數(shù)只做一件事,不要將多個(gè)功能集中在同一個(gè)函數(shù)中,不要大而全,要小而精。這樣,當(dāng)有需求變化時(shí),我們只需要修改對(duì)應(yīng)的部分即可,程序應(yīng)對(duì)變化的能力明顯提升。

開放封閉原則是指對(duì)擴(kuò)展開放,對(duì)修改關(guān)閉。

寫程序的都知道,甲方是善變的,今天說用這種方式實(shí)現(xiàn),明天可能就變卦了,這太正常了。所以我們寫程序時(shí)一定要注意程序的可擴(kuò)展性,當(dāng)甲方改動(dòng)需求時(shí),我們盡可能的少改動(dòng)或者不改動(dòng)原有代碼,而是通過添加新的實(shí)現(xiàn)類來(lái)擴(kuò)展功能,這意味著你系統(tǒng)的原有功能是不會(huì)遭到破壞的,則穩(wěn)定性有極大提升。

接口隔離原則是指調(diào)用方不應(yīng)該依賴其不需要的接口,接口間的依賴關(guān)系應(yīng)當(dāng)建立在最小功能接口原則之上。

單一職責(zé)和接口隔離都是為了提高類的內(nèi)聚性,降低他們之間的耦合性。這是面向?qū)ο蠓庋b思想的完美體現(xiàn)。

0x02 使用 with

平時(shí)寫代碼難免會(huì)遇到操作文件的需求,一般都是用 open() 函數(shù)來(lái)打開一個(gè)文件,最后等操作完成之后通過 close() 函數(shù)來(lái)關(guān)閉文件,但有時(shí)候?qū)懚嗔穗y免會(huì)覺得很麻煩,難道不可以在我操作完自動(dòng)關(guān)閉文件么,可以的。使用 with 來(lái)操作文件無(wú)需考慮關(guān)閉問題,我們只需要關(guān)心核心的業(yè)務(wù)邏輯即可。

with open('tmp.txt', 'w') as f:
    f.write('xxx')
    ...

0x03 使用 get

妙不可言!寫出優(yōu)雅的 Python 代碼的七條重要技巧

當(dāng)我們從字典中獲取一個(gè)不存在的 key 時(shí),如果是用中括號(hào)的方式來(lái)獲取的話程序會(huì)返回 KeyError。這時(shí)候建議通過 get() 函數(shù)來(lái)獲取。

同時(shí)通過 get() 函數(shù)來(lái)獲取 value 時(shí)還可以設(shè)置默認(rèn)值 default_value,當(dāng) key 不存在時(shí)則會(huì)返回 default_value。

0x04 提前返回

平時(shí)寫的代碼中少不了 if else 等控制語(yǔ)句,但有時(shí)候有的小伙伴喜歡將 if else 嵌套好多層,過幾個(gè)月之后自己都看不明白當(dāng)時(shí)寫的啥。

比如下面這個(gè)程序,根據(jù)考試成績(jī)來(lái)做評(píng)級(jí)。

score = 100 if score >= 60: # 及格 if score >= 70: # 中等 if score >= 80: # 良好  if score >= 90: # 優(yōu)秀 if score >= 100: # 滿分 print("滿分") else: print("優(yōu)秀") else: print("良好") else: print("中等") else: print("及格") else: print("不及格") print("程序結(jié)束")

這種代碼一看就想打人有木有,可讀性極差。

代碼的邏輯就是判斷分?jǐn)?shù)是否在一個(gè)區(qū)間,然后給出與之相匹配的評(píng)級(jí),既然如此,則可以改寫如下:

def get_score_level(score): if score >= 100: # 滿分 print("滿分") return if score >= 90: # 優(yōu)秀 print("優(yōu)秀") return if score >= 80: # 良好 print("良好") return if score >= 70: # 中等 print("中等") return if score >= 60: # 及格 print("及格") return print("不及格")
    print("程序結(jié)束")

這種處理方式是極其優(yōu)雅的,從上往下清晰明了,大大增加了代碼的可讀性和可維護(hù)性。

0x05 生成器

我們都知道通過列表生成式可以直接創(chuàng)建一個(gè)新的列表,但受機(jī)器內(nèi)存限制,列表的容量肯定是有限的。如果列表里面的數(shù)據(jù)是通過某種規(guī)律推導(dǎo)計(jì)算出來(lái)的,那是否可以在迭代過程中不斷的推算出后面的元素呢,這樣就不必一次性創(chuàng)建完整個(gè)列表,按需使用即可,這時(shí)候生成器就派上用場(chǎng)了。

妙不可言!寫出優(yōu)雅的 Python 代碼的七條重要技巧

0x06 裝飾器

試想一下如下的場(chǎng)景,當(dāng)后端接收到用戶請(qǐng)求后,需要對(duì)用戶進(jìn)行鑒權(quán),總不能將鑒權(quán)的代碼復(fù)制來(lái)復(fù)制去吧;還有我們的項(xiàng)目都是需要記錄日志的,這兩種情況最適合使用裝飾器。事實(shí)上 Flask 框架中就大量使用裝飾器來(lái)進(jìn)行鑒權(quán)操作。

一切皆對(duì)象!

在 Python 中我們可以在函數(shù)中定義函數(shù),也可以從函數(shù)中返回函數(shù),還可以將函數(shù)作為參數(shù)傳給另一個(gè)函數(shù)。

def hi(name="yasoob"): print("now you are inside the hi() function") def greet(): return "now you are in the greet() function" def welcome(): return "now you are in the welcome() function" print(greet())
    print(welcome())
    print("now you are back in the hi() function")

hi() # output # now you are inside the hi() function # now you are in the greet() function # now you are in the welcome() function # now you are back in the hi() function 

在上面的代碼中,我們?cè)?nbsp;hi() 函數(shù)內(nèi)部定義了兩個(gè)新的函數(shù),無(wú)論何時(shí)調(diào)用 hi() 其內(nèi)部的函數(shù)都將會(huì)被調(diào)用。

def hi(name="yasoob"): def greet(): return "now you are in the greet() function" def welcome(): return "now you are in the welcome() function" if name == "yasoob": return greet else: return welcome
 
a = hi()
print(a)
print(a()) # output # <function hi.<locals>.greet at 0x7fe3e547a0e0> # now you are in the greet() function 

在這個(gè)例子中,由于默認(rèn)參數(shù) name = yasoob 因此 a = hi() 返回的是 greet 函數(shù)。a 也就指向了 hi() 函數(shù)內(nèi)部的 greet() 函數(shù)。

def hi(): return "hi yasoob!" def doSomethingBeforeHi(func): print("I am doing some boring work before executing hi()")
    print(func())
 
doSomethingBeforeHi(hi) # output # I am doing some boring work before executing hi() # hi yasoob! 

在最后這個(gè)例子中,我們將 hi() 函數(shù)傳遞給了另外一個(gè)函數(shù),并且他們還很愉快的執(zhí)行了。

現(xiàn)在,讓我們來(lái)看看 Python 中的裝飾器吧。

def a_new_decorator(a_func): def wrapTheFunction(): print("I am doing some boring work before executing a_func()")
 
        a_func()
 
        print("I am doing some boring work after executing a_func()") return wrapTheFunction def a_function_requiring_decoration(): print("I am the function which needs some decoration to remove my foul smell")

 
a_new_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)

a_new_function_requiring_decoration() # output # I am doing some boring work before executing a_func() # I am the function which needs some decoration to remove my foul smell # I am doing some boring work after executing a_func() 

看懂了沒,就是上面我們介紹的基礎(chǔ)操作的組合。事實(shí)上這就是 python 中的裝飾器所做的事,通過這種方式來(lái)修改一個(gè)函數(shù)的行為。

但如果每次都這么寫的話未免也太麻煩了吧,因此 python 為我們提供了一個(gè)便捷操作 @。

def a_new_decorator(a_func): ... @a_new_decorator def a_function_requiring_decoration(): print("I am the function which needs some decoration to remove my foul smell")

a_function_requiring_decoration() # output # I am doing some boring work before executing a_func() # I am the function which needs some decoration to remove my foul smell # I am doing some boring work after executing a_func() 

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }