Python Glossary, library synonymous with package? Module synonymous with class?

Asked

Viewed 712 times

0

I’m studying Python with video lessons, and the teachers use many synonyms that seem the same to me.

In the case of these two terms are the same thing? Module by what I found right here would be any.py file, even the package is a.py file, ie a module is a class?

I’m sorry if I’m asking a stupid question.

1 answer

3

This terminology is not always used strictly everywhere - although they have a common meaning, there are differences:

Class (class) - is the most distinct term from the others: class is a single class in Python, which is usually defined within a Python source file, with the command class (but can also be created in C code, or created dynamically, without having a command class and a corresponding block). The important thing is that unlike other languages, there is no limitation of how many classes can be created in a file, nor any mandatory match between the file name and the name of any class within it. It is generally recommended, for reasons of style and convention, that class names be in Camelcase (but this is not mandatory).

module - this yes, strictly speaking is the name given to everything that is defined in a single file ". py"

bundle - Name given to a directory (or other set, such as a zip file) containing multiple .py. files In general a folder will contain at least one file __init__.py (even if empty, with zero bytes) to be considered a package.

library (library) - that name has a slightly less precise definition - can designate a project external, contained in a single module, or in a package, or even in a set of packages. "Libraries" are third-party projects, which are not directly part of the language, and which are installed by copying the necessary files to specific system folders. This installation is usually done automatically by tools such as pip, conda or an installer of its own. But it is not incorrect to also call "library" a specific module or package within the "standard library". Examples of libraries: requests, numpy, pandas, Pillow.

standard library ("standard library", often abbreviated to stdlib): set of packages and modules accompanying a standard Python installation, and are considered as "part of the language" - examples: math, http.server, pickle. You don’t need to install anything other than Python to have the standard library features, which can be imported whenever needed.

When you are using a library, it does not matter if it has been defined as a single module or in a package with multiple files. But it is important to know whether you imported a module from within a package, or whether you imported a class. In general the hint is in the names - otherwise, you can only count on the documentation.

For example: from concurrent.futures import ThreadPoolExecutor imports a class. But the case of capital is not always a rule, and sometimes it does not matter. For example: in from datetime import datetime - the first datetime may be set in a single file, in which case it is a module, or in a folder named datetime, case in which is a package - and it doesn’t really matter. Already the according to datetime is a class, but is less used to create instances directly (with the call datetime(ano, dia, mes, hora, minutos, segundos)) than to call several of the class methods contained within it (dier: within the datetime class) - for example, the methods datetime.strptime, datetime.now, are all class methods that create a new datetime object, but could perfectly be functions within a module datetime: for those who are using, it makes no difference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.