Python module and Python package - My 3rd week of learning python by Nishant Kalanta


This is my third week of learning Artificial Intelligence/Data Science. I have started Python which is my first step towards learning Machine learning. Like last week I told you about how to focus and a week before that is told you few steps needed to follow during the execution of the program. Today I a going to tell you all about the next topic that we studied while studying python i.e. “Modules”. 

A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference. Simply, a module is a file consisting of Python code. A module can define functions, classes, and variables. A module can also include runnable code. To support this, Python has a way to put definitions in a file and use them in a script or an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).

Following are the basic function used by modules in python language:

1.   The import statement

We can use any Python source file as a module by executing an import statement in some other Python source files.

When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches for importing a module.

2. The dir() function

The dir() built-in function returns a sorted list of strings containing the names defined by a module.

Packages

Packages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name A.B designates a submodule named B in a package named A. Just like the use of modules saves the authors of different modules from having to worry about each other’s global variable names, the use of dotted module names saves the authors of multi-module packages like NumPy or Pillow from having to worry about each other’s module names.


1.   Importing * From a Package

Now, what happens when the user writes from sound. effects import *? Ideally, one would hope that this somehow goes out to the filesystem, finds which submodules are present in the package, and imports them all. This could take a long time and importing sub-modules might have unwanted side-effects that should only happen when the sub-module is explicitly imported.

At last, I want to say that any Python file is a module, its name being the file's base name without the .py extension. A package is a collection of Python modules: while a module is a single Python file, a package is a directory of Python modules containing an additional __init__.py file, to distinguish a package from a directory that just happens to contain a bunch of Python scripts. Packages can be nested to any depth, provided that the corresponding directories contain their own __init__.py file.

0 views

Contact Us:

A-116, The Corenthum A-65,

Sector 62

Noida 201301

UP, India

Phone: +91 8882050481

Email: info@theikigailab.com

© 2020 TheIkigaiLab  Terms of Use Privacy and Security Statement