在Python中一切都是对象。从来源分,对象要分三类:内置对象、标准库对象、扩展库对象。
内置对象和标准库对象是 Python 运行程序自带的,可以说是Python 运行程序的组成部分,在安装 Python 运行程序时,也就同时安装了。扩展库对象不是 Python 运行程序自带的,需要先安装才能使用。
一、内置对象
内置对象是 Python 运行程序自带的,没什么特别的限制,直接使用即可。例如, property()、classmethod()、staticmethod() 这三个函数,就是内置函数,可以直接用。
所有的内置对象都定义在 builtins 模块中,用 dir(builtins) 语句可以查看所有的内置对象名。例如:
print(dir(__builtins__))
# 显示
# ['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', 'delattr', 'dict', 'dir',
# 'divmod', 'enumerate', 'eval', 'exec', 'filter', 'float', 'format', 'frozenset',
# 'getattr', 'globals', 'hasattr', 'hash', 'hex', 'id', 'input', 'int',
# 'isinstance', 'issubclass', 'iter', 'len', 'list', 'locals', 'map', 'max',
# 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print',
# 'property', 'range', 'repr', 'reversed', 'round', 'set','setattr', 'slice',
# 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
二、标准库对象
标准库对象与内置对象一样,是 Python 运行程序自带的,但标准库对象分门别类定义在众多的标准库模块中,在使用标准库对象之前,必须要用“import”语句导入相应的标准库模块。
从以上“print(dir(builtins))”可见,内置对象的数量是很多的,但与标准库对象比起来,又是小巫见大巫了。标准库对象的数量是庞大的,把所有的标准库对象放在一个文件中,显然不现实。像内置对象那样,把所有的标准库对象都在启动 Python 运行程序时全部导入,也没有必要,因为有许多的标准库对象是专业性的,并不常用。所以,标准库对象是分类存放,需要什么就导入什么,不需要的就不用管它,让它“呆在外面”。
那么标准库对象一共有哪些呢?它们都存放在哪些模块中呢?因为标准库对象实在太多,不可能三言两语能说清楚,需要查看 Python 的标准库说明文档来了解所有标准库对象的存在及分布情况。
Python的标准库说明文档在:https://docs.python.org/zh-cn/3/library/index.html
在说明文档中,除了前面部分介绍的“内置函数、内置常量、内置类型、内置异常”属于内置对象外,其它部分介绍的都是标准模块及各模块中定义的标准库对象。
例如,在说明文档中,可以看到在“数据类型”部分有:datetime — 基本的日期和时间类型。这说明存在一个存放“基本的日期和时间类型”类的模块:datetime。点击“datetime — 基本的日期和时间类型”,可以打开一个新的页面,其中有对 datetime 的详细介绍。
dir(模块名),可以查看到指定模块中定义的所有对象。
三、扩展库对象
扩展库对象是第三方设计制作的对象,这个是动态的,Python 运行程序不可能自带。在使用前首先要下载安装相应的扩展库,然后用“import”语句导入之后才能使用。
扩展库就像各种应该软件一样五花八门。我们自己也可以制作扩展库供自己使用,或在网络上分享供他人使用。