Python中的模块和库有什么区别? [英] Whats the difference between a module and a library in Python?

查看:28
本文介绍了Python中的模块和库有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Java 背景,而且我是 Python 新手.在继续之前,我想确保我正确理解 Python 术语.

I have background in Java and I am new to Python. I want to make sure I understand correctly Python terminology before I go ahead.

我对模块的理解是:一个可以被很多脚本导入的脚本,让阅读更容易.就像在 Java 中一样,您有一个类,并且该类可以被许多其他类导入.

My understanding of a module is: a script which can be imported by many scripts, to make reading easier. Just like in java you have a class, and that class can be imported by many other classes.

我对的理解是:一个库包含许多模块,这些模块按用途分开.

My understanding of a library is: A library contains many modules which are separated by its use.

我的问题是:库是不是像包一样,你有一个包,例如称为food,然后:

My question is: Are libraries like packages, where you have a package e.g. called food, then:

  • 巧克力.py
  • sweet.py
  • biscuts.py

是否包含在 food 包中?

或者图书馆使用包,所以如果我们有另一个包drink:

Or do libraries use packages, so if we had another package drink:

  • 牛奶.py
  • juice.py

包含在包中.library 包含两个包?

contained in the package. The library contains two packages?

此外,应用程序编程接口 (API) 通常包含一组位于层次结构顶部的库:

Also, an application programming interface (API) usually contains a set of libraries is this at the top of the hierarchy:

  1. API
  2. 图书馆
  3. 包装
  4. 模块
  5. 脚本

所以一个 API 将包含 2-5 个?

So an API will consist off all from 2-5?

推荐答案

来自 Python 教程 - 模块

  • 模块:

模块是包含 Python 定义和语句的文件.文件名是附加后缀 .py 的模块名称.

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.

  • 包装:

    包是一种通过使用带点的模块名称"来构建 Python 模块命名空间的方法.

    Packages are a way of structuring Python’s module namespace by using "dotted module names".

  • 如果您阅读了关于 import 语句的文档,则会提供更多详细信息,例如:

    If you read the documentation for the import statement gives more details, for example:

    Python只有一种模块对象,所有模块都是这种类型,无论模块是用 Python、C 还是别的东西.帮助组织模块并提供命名层次结构,Python 有包的概念.

    Python has only one type of module object, and all modules are of this type, regardless of whether the module is implemented in Python, C, or something else. To help organize modules and provide a naming hierarchy, Python has a concept of packages.

    您可以将包视为文件系统上的目录,并且模块作为目录中的文件,但不要也用这个类比从字面上看,因为包和模块不需要来自文件系统.出于本文档的目的,我们将使用这个目录和文件的方便类比.像文件系统目录,包是分层组织的,包可以本身包含子包以及常规模块.

    You can think of packages as the directories on a file system and modules as files within directories, but don’t take this analogy too literally since packages and modules need not originate from the file system. For the purposes of this documentation, we’ll use this convenient analogy of directories and files. Like file system directories, packages are organized hierarchically, and packages may themselves contain subpackages, as well as regular modules.

    重要的是要记住所有的包都是模块,但不是所有模块都是包.或者换句话说,包只是一个特殊类型的模块.具体来说,任何包含__path__ 属性被认为是一个包.

    It’s important to keep in mind that all packages are modules, but not all modules are packages. Or put another way, packages are just a special kind of module. Specifically, any module that contains a __path__ attribute is considered a package.

    因此术语 module 指的是一个特定的实体:它是一个类,其实例是您在 Python 程序中使用的 module 对象.以此类推,它还用于指代创建"这些实例的文件系统中的文件.

    Hence the term module refers to a specific entity: it's a class whose instances are the module objects you use in python programs. It is also used, by analogy, to refer to the file in the file system from which these instances "are created".

    术语 script 用于指代要执行其目标的模块.它与程序"或应用程序"具有相同的含义,但它通常用于描述简单的小程序(即最多几百行的单个文件).编写脚本需要几分钟或几个小时.

    The term script is used to refer to a module whose aim is to be executed. It has the same meaning as "program" or "application", but it is usually used to describe simple and small programs(i.e. a single file with at most some hundreds of lines). Writing a script takes minutes or few hours.

    术语只是一组代码的通用术语,这些代码旨在为许多应用程序使用而设计.它提供了一些可供特定应用程序使用的通用功能.

    The term library is simply a generic term for a bunch of code that was designed with the aim of being usable by many applications. It provides some generic functionality that can be used by specific applications.

    当一个模块/包/其他东西被发布"时,人们通常把它称为一个库.库通常包含一个包或多个相关包,但它甚至可以是单个模块.

    When a module/package/something else is "published" people often refer to it as a library. Often libraries contain a package or multiple related packages, but it could be even a single module.

    库通常不提供任何特定功能,即您不能运行库".

    Libraries usually do not provide any specific functionality, i.e. you cannot "run a library".

    API 可以根据上下文具有不同的含义.例如:

    The API can have different meanings depending on the context. For example:

    • 它可以定义像 DB API缓冲协议.
    • 它可以定义如何与应用程序交互(例如 Python/C API)
    • 当与库/包相关时,它只是该库为其功能(一组函数/类/常量等)提供的接口

    无论如何,API 不是 python 代码.这是一个或多或少正式的描述.

    In any case an API is not python code. It's a description which may be more or less formal.

    这篇关于Python中的模块和库有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆