添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
暴走的树叶  ·  n2adr-sdr@groups.io | ...·  2 天前    · 
睡不着的盒饭  ·  xml.sax.saxutils --- ...·  昨天    · 
才高八斗的豆浆  ·  [python] ...·  19 小时前    · 
健壮的冲锋衣  ·  大纲送至剪贴板·  2 周前    · 
被表白的遥控器  ·  List of member ...·  4 月前    · 
完美的砖头  ·  How to Serialize a ...·  6 月前    · 
冷静的萝卜  ·  Uncaught (in promise) ...·  7 月前    · 

builtins --- 內建物件

該模組提供對 Python 所有'內建'識別符號的直接存取;例如 builtins.open 是內建函式 open() 的全名。請參閱 內建函式 內建常數 的文件。

大多數應用程式通常不會顯式地存取此模組,但在提供與內建值同名之物件的模組中可能很有用,不過其中還會需要內建該名稱。例如,在一個將內建 open() 包裝起來以實現另一版本 open() 函式的模組中,這個模組可以直接被使用:

import builtins
def 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 = f
    def read(self, count=-1):
        return self._f.read(count).upper()
    # ...

有個實作細節是,大多數模組都將名稱 __builtins__ 作為其全域性變數的一部分以提使用。__builtins__ 的值通常是這個模組或者這個模組的 __dict__ 屬性值。由於這是一個實作細節,因此 Python 的其他實作可能不會使用它。

2001-2024, Python Software Foundation. This page is licensed under the Python Software Foundation License Version 2. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License. See History and License for more information.