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

查看:300
本文介绍了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:

  • chocolate.py
  • sweets.py
  • biscuts.py
  • chocolate.py
  • sweets.py
  • biscuts.py

是否包含在food包中?

还是图书馆使用软件包,所以如果我们有另一个软件包drink:

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

  • milk.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的模块名称.

  • 包装:

    包是一种通过使用点分模块名称"来构造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只有一种类型的 module对象,并且所有模块都属于这种类型 类型,无论该模块是用Python,C还是Windows实现的 其他的东西.帮助组织模块并提供命名 在层次结构中,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.

    术语 library 只是一堆代码的通称,其目的是为可被许多应用程序使用.它提供了一些可以由特定应用程序使用的通用功能.

    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 )
    • 与库/程序包相关时,它只是该库提供的用于其功能(功能/类/常量等的集合)的接口
    • it can define a protocol like the DB API or the buffer protocol.
    • it can define how to interact with an application(e.g. the Python/C API)
    • when related to a library/package it simply the interface provided by that library for its functionality(set of functions/classes/constants etc.)

    在任何情况下,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天全站免登陆