×

Introduction

To begin with, Python Python is one of the trendy programming languages. It is pretty easy to understand and used to develop advanced applications with evolving circumstances. The demand for knowledge and skills in Python is growing and most companies are looking for skilled Python developers. Most importantly, Python programming is being increasingly used in several industries like web development, machine learning, Internet of Things,Data science with its powerful libraries.

What are Python Modules?

In simple terms, Python Module is a file that consists of python code(executable statements) to perform a specific task.The Python standard library contains well over 200+ modules.Python modules include runnable code that define functions, classes and variables.

The import Statement

Basically, the import statement is used to transfer all of the python code from one file to another Python file.The Python code in one module can be accessed by another module using the importing process. The import statement is the most common way of doing it. Functions such as importlib.import_module() and built-in __import__() can also be used to perform the importing process. Importing is done in the header section of the code before writing any specific functions.We import a module to make the code available in the current program as a separate namespace.

Example:

import math math.pow(2,4) The “from import” statement

To import a specific function, Use from … import ….. This method of importing allows us to call the function without using the dot-notation.

Example:

from math import cos, pi print(‘cos(pi) is’, cos(pi))

When we use import x, it will create a reference to that module in the current namespace of x. So we need to define a module path to access a particular attribute or method from that module. On the other hand, if we uses from x import *, it creates a reference to all public objects in x within the local namespace of the current module. We won’t have to define a module path such as x.name, but can simply refer to it as name.

Points to be noted:
  • Imports should be written at the top of the file after any comments and docstrings.
  • Use blank space to divide the group of imports
  • Write your imports alphabetically
  • The three import groups are:

  • standard library imports with built-in modules,
  • Related third party imports – modules that are installed but not belong to the current application,
  • local application imports – modules that belong to the current application)
  • The “dir()” function

    The function dir python is responsible for returning the valid list of the attributes for the passed objects in the current local scope. It can have one optional parameter.

    dir() syntax

    dir(object) object – can be any user-defined object or tuple, list, set, dictionary etc.

    Examples:

  • number1 = [1, 2, 3]
  • number2=[]
  • #dir with a list
  • print(dir(number1))
  • # dir() with a set
  • print(dir(number))
  • # dir() with an empty tuple
  • print(dir(number1))
  • class Junior:
  • def __dir__(self):
  • return [‘id’, ‘name’, ‘class’]
  • student = Junior()
  • print(dir(student))
  • The globals() and locals() functions

    Python locals() function is the built-in function that returns the dictionary of the current local symbol table.The local symbol table stores the information needed for the local scope of the program. It does not include any input parameter.

    Syntax : locals()

    Python globals() function is the built-in function that returns the dictionary of the current global symbol table. The global symbol table stores the information related to the global scope of the program.

    Syntax: globals()

    Symbol table:

    Python interpreter maintains a data structure containing information about each identifier in the source code. This information (symbol) is about the type, value, scope level, and location of an identifier. The local symbol table stores the information needed for the local scope of the program.The global symbol table stores the information related to the global scope of the program.

    Uses of these symbol tables:

    • store tall entities and retrieve them efficiently.
    • verify if an object has been declared.
    • determine the scope of an object.
    • type checking and semantic accuracy.

    use a single module to hold all the global variables to be used. import the module to use the global variables. If you modify them then it will be visible in other modules

    The reload() function

    The Python reload() function is used to reload a previously imported or loaded module. In a test environment, when you repeatedly run a test script, reload the modules to make use of the changes made in the code.The argument passed to the reload() must be a module object which was imported before.

    • Python module’s code can be recompiled and re-executed with a new set of objects in the module’s dictionary. The init function of the modules can not be loaded again.
    • The old objects are reclaimed when their reference count becomes zero.
    • The names in the module namespace are changed to new objects if any.
    • Example:
    • import math
    • import importlib
    • importlib.reload(math)

    Conclusion

    To sum up, Python is the most popular programming language which is in the upgrowing path. The demand for Python professionals is growing and used in top trending industries. Master python to start a successful career path with its powerful libraries. Choose the right python libraries to upskill yourself based on your dream career. Enroll in Python training in Chennai to develop the industrial skill set with assured placement support.

    Trending Courses

    DevOps Training in Chennai | Salesforce Training in Chennai | Machine Learning Training in Chennai | Python Training in Chennai | Primavera Training in Chennai | PMP Training in Chennai | AWS Training in Chennai | Full Stack Developer Course in Chennai | Selenium Training in Chennai