1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python 内置函数 内置模块 标准库 第三方库 函数 模块 包 库

python 内置函数 内置模块 标准库 第三方库 函数 模块 包 库

时间:2022-04-09 03:03:10

相关推荐

python 内置函数 内置模块 标准库 第三方库 函数 模块 包 库

大家在学习python时,会遇到这样的情况,有些东西直接就可以使用,例如str()、len();

而有些需要import 导入才能使用,比如sys、os、time、math;

还有些需要安装才能使用,比如pandas、pyautoGUI、numpy、requests。

然后还分不清什么是函数,什么是模块,什么是包,什么又是库。

编程重在理解与运用,同学们大可不必纠结于概念,我们了解下即可。

内建对象——不需要导入

Python解释器自带的函数、常量分别叫做内置函数、内置常量,这些函数和常量可以直接使用,不需要定义或import导入。

这里引入的概念【内置函数】、【内置常量】

【内置函数】一般包括Python 解释器自带的函数(function)和类型(class)

function -- 函数是可以向调用者返回某个值的一组语句。还可以向其传入零个或多个参数并在函数体执行中被使用。

参考官方文档: 内置函数 — Python 3.10.2 文档/zh-cn/3/library/functions.html#built-in-funcs

【内置常量】“常量”即不变化的量,与“变量”相对。

参考官方文档: 内置常量 — Python 3.10.2 文档/zh-cn/3/library/constants.html

使用python代码dir(__builtins__)可以输出Python所有内建对象:

(以下是我的python3.8版本输出的)

['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']

模块——需要import导入

【模块】(Module)即单独的一个python文件,模块能定义函数,类和变量,模块里也能包含可执行的代码。它是 Python 代码的一种组织单位。各模块具有独立的命名空间,可包含任意 Python 对象。模块可通过 import 操作被加载到 Python 中。

Python 中(至少)有三类模块:

使用 Python 编写的模块(.py);

使用 C 编写的动态加载模块(.dll,.pyd,.so,.sl 等);

使用 C 编写并链接到解释器的模块,要获取此列表,输入:

import sysprint(sys.builtin_module_names)

(以下是我的python3.8版本输出的)

('_abc', '_ast', '_bisect', '_blake2', '_codecs', '_codecs_cn', '_codecs_hk', '_codecs_iso', '_codecs_jp', '_codecs_kr', '_codecs_tw', '_collections', '_contextvars', '_csv', '_datetime', '_functools', '_heapq', '_imp', '_io', '_json', '_locale', '_lsprof', '_md5', '_multibytecodec', '_opcode', '_operator', '_pickle', '_random', '_sha1', '_sha256', '_sha3', '_sha512', '_signal', '_sre', '_stat', '_statistics', '_string', '_struct', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', '_winapi', '_xxsubinterpreters', 'array', 'atexit', 'audioop', 'binascii', 'builtins', 'cmath', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'math', 'mmap', 'msvcrt', 'nt', 'parser', 'sys', 'time', 'winreg', 'xxsubtype', 'zlib')

如果找不到模块的源文件,可能它是一个内建的模块,或是使用 C,C++ 或其他编译型语言实现的动态加载模块。这种情况下可能是没有源码文件的,类似mathmodule.c这样的文件会存放在 C 代码目录中(但不在 Python 目录中)。

通过 python 代码 sys.modules (一个全局字典)可以输出 python 中的所有模块对象。sys.modules是python启动后就加载在内存中。每当程序员导入新的模块,sys.modules将自动记录该模块。当第二次再导入该模块时,python会直接到字典中查找,从而加快了程序运行的速度。它拥有字典所拥有的一切方法。

{'sys': <module 'sys' (built-in)>,'builtins': <module 'builtins' (built-in)>,'_frozen_importlib': <module 'importlib._bootstrap' (frozen)>,'_imp': <module '_imp' (built-in)>,'_warnings': <module '_warnings' (built-in)>,'_frozen_importlib_external': <module 'importlib._bootstrap_external' (frozen)>,'_io': <module 'io' (built-in)>,'marshal': <module 'marshal' (built-in)>,'nt': <module 'nt' (built-in)>,'_thread': <module '_thread' (built-in)>,'_weakref': <module '_weakref' (built-in)>,'winreg': <module 'winreg' (built-in)>,'time': <module 'time' (built-in)>,'zipimport': <module 'zipimport' (frozen)>,'_codecs': <module '_codecs' (built-in)>,'codecs': <module 'codecs' from 'c:\\users\\administrator\\appdata\\local\\programs\\python\\python38\\lib\\codecs.py'>,'encodings.aliases': <module 'encodings.aliases' from 'c:\\users\\administrator\\appdata\\local\\programs\\python\\python38\\lib\\encodings\\aliases.py'>,'encodings': <module 'encodings' from 'c:\\users\\administrator\\appdata\\local\\programs\\python\\python38\\lib\\encodings\\__init__.py'>,'encodings.utf_8': <module 'encodings.utf_8' from 'c:\\users\\administrator\\appdata\\local\\programs\\python\\python38\\lib\\encodings\\utf_8.py'>,'_codecs_cn': <module '_codecs_cn' (built-in)>,'_multibytecodec': <module '_multibytecodec' (built-in)>,'encodings.gbk': <module 'encodings.gbk' from 'c:\\users\\administrator\\appdata\\local\\programs\\python\\python38\\lib\\encodings\\gbk.py'>,'_signal': <module '_signal' (built-in)>,'__main__': <module '__main__'>,'encodings.latin_1': <module 'encodings.latin_1' from 'c:\\users\\administrator\\appdata\\local\\programs\\python\\python38\\lib\\encodings\\latin_1.py'>,'_abc': <module '_abc' (built-in)>,'abc': <module 'abc' from 'c:\\users\\administrator\\appdata\\local\\programs\\python\\python38\\lib\\abc.py'>, ...}

包——需要import导入

【包】(package)是一种可包含子模块或递归地包含子包的 Python模块。从技术上说,包是带有__path__属性的 Python 模块。简单来说,它更像是一个文件夹。

库——需要import导入

【库】(Library)是具有相关功能模块的集合。这也是Python的一大特色之一,即具有强大的标准库、第三方库以及自定义模块。

标准库——需要import导入

Python 标准库相当于解释器的外部扩展,在我们安装 python 解释器的时候,就一块安装上了。它并不会随着解释器的启动而启动,要想使用这些外部扩展,必须提前导入。

Python 标准库非常庞大,所提供的组件涉及范围十分广泛。它包含了多个内置模块 (以 C 编写),Python 程序员必须依靠它们来实现系统级功能,例如文件 I/O,此外还有大量以 Python 编写的模块,提供了日常编程中许多问题的标准解决方案。其中有些模块经过专门设计,通过将特定平台功能抽象化为平台中立的 API 来鼓励和加强 Python 程序的可移植性。

Windows 版本的 Python 安装程序通常包含整个标准库,往往还包含许多额外组件。对于类 Unix 操作系统,Python 通常会分成一系列的软件包,因此可能需要使用操作系统所提供的包管理工具来获取部分或全部可选组件。

参考官方文档:Python 标准库 — Python 3.10.2 文档/zh-cn/3.10/library/index.html

第三方库——需要先安装后 再导入

在标准库以外还存在成千上万并且不断增加的其他组件 (从单独的程序、模块、软件包直到完整的应用开发框架),访问官网Python 包索引即可获取这些第三方包。

它一般放在python安装目录下的 Lib\site-packages 目录下。

自定义模块、包——需要先放到python环境变量目录下后 再导入

自定义模块、包 就是我们自己写的python模块或者是包了。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。