Can Python's standard library's written-in-Python modules be used without importing?

后端 未结 1 988
一生所求
一生所求 2020-12-07 04:46

I\'ve been having some difficulty with modules in Python. I understood that built-in modules, which are written in C, are like part of the Python interpreter, so they\'re ma

相关标签:
1条回答
  • 2020-12-07 05:15

    TLDR: You have to import every module before usage, with the exception of builtins.


    There are two different kinds of "builtin" as far as Python is concerned:

    1. Builtin objects that are compiled, such as int or os.chmod.
    2. The builtins module containing a basic set of objects, such as int and help.

    Things can be part of 1 without being part of 2 and vice versa. For example, help is a pure-Python function, and os.chmod is part of the os module. However, most of the stuff in 2 is also part of 1 for practical reasons: builtins are used a lot and compiled objects usually have better performance.


    Builtin objects can represent any kind of module, function or object. The language specification does not really define which objects are builtins and what builtins are. Details vary by implementation: CPython uses compiled C structs and functions, whereas PyPy uses compiled RPython objects, for example.

    As a rule of thumb, "everything not defined by Python code" is a builtin. However, this is not strictly true: a builtin object can access the interpreter to create regular Python objects. All builtins have in common that they appear like regular Python objects, but their internals can follow arbitrary rules.

    Note that a builtin is not necessarily built into the interpreter. CPython provides a C API and PyPy emulates it via cpyext, allowing shared libraries to be loaded and used by the interpreter. Many third-party libraries use compiled Cython objects and functions to provide custom builtins. However, any language that can produce shared libraries compatible to the C API can be used.


    The builtins module is an actual module named builtins. All names of this module are always available in all other modules: builtins is searched whenever a name is not found in the current scope, its containing scopes, or the current module.

    This is similar but not the same as if there were a from builtins import * in every module: if builtins is changed, the builtin names reflect that.

    >>> int(5)
    5
    >>> import builtins
    >>> builtins.int = float
    >>> int(5)
    5.0
    

    For example, the _ in an interactive Python session is part of builtins. It is changed whenever a command is run.

    Disclaimer: This is just for demonstration. It is a really, really bad idea to modify builtins yourself.


    While only names from builtins are available without importing, Python usually imports several modules when it starts.

    $ python3 -c 'import sys;print(sys.modules.keys())'
    dict_keys(['sys', 'builtins', '_frozen_importlib', '_imp', '_thread', '_warnings', '_weakref', 'zipimport', '_frozen_importlib_external', '_io', 'marshal', 'posix', 'encodings', 'codecs', '_codecs', 'encodings.aliases', 'encodings.utf_8', '_signal', '__main__', 'encodings.latin_1', 'io', 'abc', '_abc', 'site', 'os', 'stat', '_stat', 'posixpath', 'genericpath', 'os.path', '_collections_abc', '_sitebuiltins', '_bootlocale', '_locale', 'types', 'importlib', 'importlib._bootstrap', 'importlib._bootstrap_external', 'warnings', 'importlib.util', 'importlib.abc', 'importlib.machinery', 'contextlib', 'collections', 'operator', '_operator', 'keyword', 'heapq', '_heapq', 'itertools', 'reprlib', '_collections', 'functools', '_functools', 'mpl_toolkits', 'sphinxcontrib', 'sitecustomize', 're', 'enum', 'sre_compile', '_sre', 'sre_parse', 'sre_constants', 'copyreg'])
    

    Some of these are part of high-level parts of the interpreter, e.g. sys and importlib. The sitecustomize and usercustomize modules allow to prepare your environment - for example, changing the look of the interactive interpreter, setting search paths in os.environment, and similar.

    0 讨论(0)
提交回复
热议问题