Python mimetypes.init() 是一个过时的函数吗?

Is Python mimetypes.init() an outdated function?

我很难理解 init() 函数在 python mimetypes 包中的作用。 python 的最新版本是否不需要它是一个过时的功能?

根据 mimetypes module documentation:

The functions described below provide the primary interface for this module. If the module has not been initialized, they will call init() if they rely on the information init() sets up.


mimetypes.init(files=None)

Initialize the internal data structures. If given, files must be a sequence of file names which should be used to augment the default type map. If omitted, the file names to use are taken from knownfiles; on Windows, the current registry settings are loaded. Each file named in files or knownfiles takes precedence over those named before it. Calling init() repeatedly is allowed.

Specifying an empty list for files will prevent the system defaults from being applied: only the well-known values will be present from a built-in list.

它在 Python 2.7 and Python 3.x 中都有。

mimetypes.init() is useful if you want to add MIME type / extension mappings beyond the default. If you don't need to do that, then there's no need to call mimetypes.init(); just use the utility functions normally, and they'll call it themselves if necessary. If you do need to do that, aside from mimetypes.init() there's also mimetypes.read_mime_types() and mimetypes.add_type().

这适用于 Python 2 和 3。