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

熱線電話:13121318867

登錄
首頁精彩閱讀Python中模塊與包有相同名字的處理方法
Python中模塊與包有相同名字的處理方法
2018-03-28
收藏

Python中模塊與包有相同名字的處理方法

在編程開發(fā)中,個人覺得,只要按照規(guī)范去做,很少會出問題。剛開始學習一門技術(shù)時,的確會遇到很多的坑。踩的坑多了,這是好事,會學到更多東西,也會越來越覺得按照規(guī)范做的重要性,規(guī)范的制定就是用來規(guī)避問題的。有時候確實應(yīng)該聽聽有經(jīng)驗人的建議,不要一意孤行。這好像不是本文的重點,其實我重點是想表達,盡量按規(guī)范做事,這樣會少走很多彎路。

我現(xiàn)在使用的主力編程語言是 Python,在接觸 Python 至今,我感覺我踩的坑還是極少的,基本上沒有遇到什么奇怪的問題。實際上,這并不是一件好事,不踩坑,很多躺在暗處的知識點你不會了解,所以也很難成長。幸好,有一些會踩坑的同事。

一同事問我,在 Python 中,如果一個模塊和一個包同名時,是不是只能導入包,如果要導入模塊該怎么辦。他的意思大概是這樣的,在項目的同一級目錄下,有一個 foo.py 文件和一個 foo/ 目錄,如果 import foo 會導入 foo/ 的內(nèi)容而不是 foo.py 的內(nèi)容。

被問到這個問題時,我首先感覺到的是詫異,這明顯是存在歧義的。如果是我,肯定不會把模塊名和包名設(shè)計成一樣的名字,因為本質(zhì)上來說在導入的時候沒法區(qū)分到底要導入誰。除非系統(tǒng)有特別的規(guī)定,例如,規(guī)定這種情況只能導入包。

我的潛意識里認為這里應(yīng)該報錯,Python 解釋器不知道要導入誰。但是,同事告訴我,別人的代碼是這么寫的,而且在這種情況下會默認導入包。那就是可以的咯,而且解釋器已經(jīng)規(guī)定這種情況會總是導入包。

為了驗證下這一點,我寫了個簡單的項目,項目結(jié)構(gòu)如下:

.
├── main.py
└── same
 ├── api
 │ └── __init__.py
 ├── auth
 │ └── __init__.py
 ├── auth.py
 └── __init__.py

其中:

same/api/__init__/py 的內(nèi)容:    
from .. import auth

same/auth/__init__.py 的內(nèi)容:    
auth_str = "This is str in package!"

same/auth.py 的內(nèi)容:    
auth_str = "This is str in module!"

main.py 的內(nèi)容:
    
from __future__ import print_function
 
from same.api import auth
 
# Script starts from here
 
if __name__ == "__main__":
 print(auth.auth_str)
稍微有些復(fù)雜,哈哈,主要是同事那兒大致的結(jié)構(gòu)是這樣的,這里是為更好的模擬下。我在 same.auth 包中定義了一個 auth_str 字符串,又在同名的 same.auth 模塊中定義了一個同名的 auth_str 字符串,然后在 same.api 包嘗試導入 auth,最后在 main.py 嘗試輸出 same.api.auth.auth_str,看看到底哪個字符串會被打印。同時嘗試用 Python2 和 Python3 執(zhí)行 main.py,得到的結(jié)果都是:    
This is str in package!
這里驗證了我們的猜想是正確的,解釋器的確只導入了包中內(nèi)容。但是,我并不知道是否有官方的資料說明就是這樣的,所以我不敢確信,萬一這只是巧合呢。
于是,我開始查資料來驗證這一結(jié)論。我就說實話吧,對于一個英文水平爛到你無法想象的我,只能先嘗試用百度搜索下答案了。事實是,用百度往往都是遺憾的。片刻后,無果,我只能硬著頭皮嘗試英文搜索了。于是,在 stackoverflow 上找到了如下提問:
How python deals with module and package having the same name?
其中有一個人回答說 Python 官方文檔中在描述模塊搜索路徑時提到了這一點:https://docs.python.org/3/tutorial/modules.html#the-module-search-path.
文檔中有如下一段描述:
After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.
也就是說,目錄在庫的搜索路徑下會首先被搜索,這就意味著目錄會代替同名的模塊被加載。
這下終于放心了,之前的結(jié)論得到證實。在 Python 中,如果嘗試導入同名的模塊和包時,包會被導入。這種情況下,如果想要導入模塊,恐怕要用一些 ‘hack' 的方法,上面提到的 stackoverflow 帖下有一些示例可以參考。當然,最好的方法是避免這樣的設(shè)計,這樣你就不會花那么長時間去查資料,也不會花那么長時間來寫類似于本文的文章。

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

若不方便掃碼,搜微信號: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(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".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 = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+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); }