新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:builtins—-內(nèi)建對(duì)象
builtins —- 內(nèi)建對(duì)象
該模塊提供對(duì)python的所有“內(nèi)置”標(biāo)識(shí)符的直接訪問;例如,builtins.open 是內(nèi)置函數(shù)的全名 open() 。請(qǐng)參閱 內(nèi)置函數(shù) 和 內(nèi)置常量 的文檔。

大多數(shù)應(yīng)用程序通常不會(huì)顯式訪問此模塊,但在提供與內(nèi)置值同名的對(duì)象的模塊中可能很有用,但其中還需要內(nèi)置該名稱。例如,在一個(gè)想要實(shí)現(xiàn) open() 函數(shù)的模塊中,它包裝了內(nèi)置的 open() ,這個(gè)模塊可以直接使用:
import builtinsdef open(path):f = builtins.open(path, 'r')return UpperCaser(f)class UpperCaser:'''Wrapper around a file that converts output to uppercase.'''def __init__(self, f):self._f = fdef read(self, count=-1):return self._f.read(count).upper()# ...
作為一個(gè)實(shí)現(xiàn)細(xì)節(jié),大多數(shù)模塊都將名稱 __builtins__ 作為其全局變量的一部分提供。 __builtins__ 的值通常是這個(gè)模塊或者這個(gè)模塊的值 __dict__ 屬性。由于這是一個(gè)實(shí)現(xiàn)細(xì)節(jié),因此 Python 的替代實(shí)現(xiàn)可能不會(huì)使用它。
當(dāng)前標(biāo)題:創(chuàng)新互聯(lián)Python教程:builtins—-內(nèi)建對(duì)象
網(wǎng)站網(wǎng)址:http://fisionsoft.com.cn/article/dpssjgc.html


咨詢
建站咨詢
