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.

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.

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

chocolate.py

sweets.py

biscuts.py

are contained in the food package?

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

milk.py

juice.py

contained in the package. The library contains two packages?

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

API

Library

Package

Module

Script

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

解决方案Module:

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

Package:

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

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

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.

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.

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".

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".

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

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.)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值