深入解析Python中的__builtins__內(nèi)建對(duì)象
如果你已經(jīng)學(xué)習(xí)了包,模塊這些知識(shí)了。
你會(huì)不會(huì)有好奇:Python為什么可以直接使用一些內(nèi)建函數(shù),不用顯式的導(dǎo)入它們,比如 str() int() dir() ...?
原因是Python解釋器第一次啟動(dòng)的時(shí)候 __builtins__ 就已經(jīng)在命名空間了(Note: 有s)
進(jìn)Shell看看:
>>> globals() {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, '__package__': None}
你可以再次導(dǎo)入 __builtin__(Note: 沒有s):
import __builtin__ >>> globals() {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, '__builtin__': <module '__builtin__' (built-in)>, '__package__': None}
這時(shí)候多了一個(gè) __builtin__ 對(duì)象,你可以判斷它們是不是相同的:
>>> __builtin__ is __builtins__ True >>> type(__builtin__) <type 'module'> >>> type(__builtins__) <type 'module'>
現(xiàn)在我們把它從一個(gè)文件導(dǎo)入:
# file1.py import __builtin__ print "module name __name__ : ", __name__ print "__builtin__ is __builtins__: ", __builtin__ is __builtins__ print "type(__builtin__): ", type(__builtin__) print "type(__builtins__): ", type(__builtins__) print "__builtins__ is __builtin__.__dict__", __builtins__ is __builtin__.__dict__ # file2.py import file1 """結(jié)果: module name __name__ : file __builtin__ is __builtins__: False type(__builtin__): <type 'module'> type(__builtins__): <type 'dict'> __builtins__ is __builtin__.__dict__ True """
結(jié)論:
__builtins__ 是對(duì)內(nèi)建模塊 __builtin__ 的引用,并且有如下兩個(gè)方面差異:
在主模塊中,即沒有被其他文件導(dǎo)入。__builtins__是對(duì) __builtin__ 本身的引用,兩者是相同的。
通過 __builtins__ is __builtin__.__dict__ 猜想:
在非 '__main__' 模塊中,也就是模塊被導(dǎo)入后,__builtins__ 應(yīng)該屬于 __builtin__.__dict__ 的一部分,是對(duì) __builtin__.__dict__ 的引用,而非builtin本身,它在任何地方都可見,此時(shí)builtins的類型是字典。
裝飾內(nèi)建函數(shù)
Python 官方文檔 解釋了如何裝飾一個(gè)內(nèi)建函數(shù):
import __builtin__ def open(path): f = __builtin__.open(path, 'r') return UpperCaser(f) class UpperCaser: __metaclass__ = type def __init__(self, f): self._f = f def read(self): return self._f.read().upper() print open('./a.txt').read() # 將會(huì)全部轉(zhuǎn)為大寫輸出
Note:Python3.X版本中,內(nèi)建模塊更名為builtins,與Python2.X有所不同
相關(guān)文章
python實(shí)現(xiàn)beta分布概率密度函數(shù)的方法
今天小編就為大家分享一篇python實(shí)現(xiàn)beta分布概率密度函數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07Pytorch模型定義與深度學(xué)習(xí)自查手冊(cè)
這篇文章主要為大家介紹了Pytorch模型定義與深度學(xué)習(xí)的自查手冊(cè),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06使用Python通過oBIX協(xié)議訪問Niagara數(shù)據(jù)的示例
這篇文章主要介紹了使用Python通過oBIX協(xié)議訪問Niagara數(shù)據(jù)的示例,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-12-12Python使用Selenium WebDriver的入門介紹及安裝教程(最新推薦)
這篇文章主要介紹了Python使用Selenium WebDriver的入門介紹及安裝教程,本文使用環(huán)境為python3.11+win10 64位+firefox瀏覽器,所以本文使用的瀏覽器驅(qū)動(dòng)是Firefox的geckodriver ,如果你使用的是其他瀏覽器,那么選擇自己對(duì)應(yīng)的瀏覽器驅(qū)動(dòng)程序即可,需要的朋友可以參考下2023-04-04解決新版Pycharm中Matplotlib圖像不在彈出獨(dú)立的顯示窗口問題
今天小編就為大家分享一篇解決新版Pycharm中Matplotlib圖像不在彈出獨(dú)立的顯示窗口問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python實(shí)現(xiàn)圖像隨機(jī)添加椒鹽噪聲和高斯噪聲
圖像噪聲是指存在于圖像數(shù)據(jù)中的不必要的或多余的干擾信息。在噪聲的概念中,通常采用信噪比(Signal-Noise?Rate,?SNR)衡量圖像噪聲。本文將利用Python實(shí)現(xiàn)對(duì)圖像隨機(jī)添加椒鹽噪聲和高斯噪聲,感興趣的可以了解一下2022-09-09