Modules in Python


Modules

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.

A module  allows you to logically organize your python code. Grouping related code into a module makes the code easier to understand the use.

A module is the python object with arbitrarily named attributes that you can bind and reference.Consider a module to be the same as a code library.

A file containing a set of functions you want to include in your application.

Create a Module

To create a module just save the code you want in file with the file extension ".py".

Example

def  greeting(name):

        print("hello"+name)

Save the code in a file with named "mymodule.py".

Use the Module

Now we can use the module we just created by using the import statement.

Example

# import the module named mymodule, and call the greeting function.

import mymodule

mymodule.greeting("technicalguptaji")

Output

hello technicalguptaji

Note : When using a function from a module, we use the syntax -
  
module_name.function_name()

Variables in Module

The module can contain variables of all types (dictionaries,objects, int, tuple etc.).


Example

person={"name":"technicalguptaji","age":27,"country":"India"}

Save this code in the file "mymodule.py".

Import the module named mymodule, and access the person dictionary.

import mymodule

var=mymodule.person["age"]

print(var)

Output

27

Naming a Module

You can name the module file whatever you like, but it must have the file extension ".py"


Re-naming a module

You can create an alias when you import a module, by using the as keyword.

Example

import mymodule as yourmodule

a=yourmodule.person["age"]

print(a)

Output

27

Import from module

You can choose to import only parts from a module, by using the from keyword.

Example

def greeting(name):

        print("Hello"+name)

person={"name":"technicalguptaji","age":27}

The module named mymodule has one function and one dictionary.
Import only the person dictionary from the module.

from mymodule import person

print(person["age"])

Output

27

Built-in Module

There are several built-in modules in python, which you can import whenever you like.

Example : Import and use the platform module.

import Platform

x=Paltform.system()

print(x)

Here we are calling the system() method which will return the current operating system name and its placed in Platform module so we have to be import the platform module.

Using the dir() function

There is a in-built function to list all the function names or variables names in the module. The dir() function. 

Example : List all the defined names belonging to the Platform module.

import Platform

x=dir(Platform)

print(x)

It will produce a list of all the functions and variables name of Platform module.

Note : The dir() function can be used on all modules, also the ones you create yourself.




















No comments:

Post a Comment