Featured post

Why should I learn Go?

Image
What is unique about GO language? Here are some of the advantages of GO programming language:           Code runs fast           Garbage collection           Simpler objects           Efficient concurrency Code runs faster: Before understanding why GO runs faster, let us know the process of software translation. Basically, we have three broad categories of languages:             Machine level language ·        Machine level language is a low-level language where instructions are directly executed on the CPU. Machine level instructions are small steps which are straight forward and simple (Ex: ADD, SUBTRACT, MULTIPLY ) Assembly language ·        Assembly language is similar to machine level language but a bit more specific for humans to understand. For example, 1000...

Modules in Python






We, ‘pygrammers’ (python programmers) knew about the great python interpreter. We also know that whatever we type into the interpreter vanishes as soon as we close the interpreter.

 So if you want to use some of the definitions and statements in other programs without rewriting the definition, we use a special type of container called the ‘MODULE’.

The process of creating definitions and saving it in a file is called “writing a script”.

A module is a python file ( filename.py) which contains statements and some definitions.

Why Modules?

Imagine that you want to build an application which consists of a million lines of code. As  an efficient programmer you must use the concept of ‘re-use’ because unnecessarily writing the same code multiple times will not only lead to increase in the length of the program but also effects the program's efficiency.

To support this, Python has a way to put definitions in a file and use them in a script or in 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.

For instance, let us write the following code using a text editor and saving with a file name with the extension '.py'

# Fibonacci numbers module

def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while b < n:
        print(b, end=' ')
        a, b = b, a+b
    print()

def fib2(n):   # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while b < n:
        result.append(b)
        a, b = b, a+b
    return result

Now enter the Python interpreter and import this module with the following command:

>>> 
>>> import fibo

This does not enter the names of the functions defined in fibo directly in the current symbol table; it only enters the module name fibo there. Using the module name you can access the functions:

>>> 
>>> fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> fibo.fib2(100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> fibo.__name__
'fibo'



If you intend to use a function often you can assign it to a local name:

>>> 
>>> fib = fibo.fib
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377


The examples have been taken from the Official documentation.

Popular posts from this blog

Introduction to Big Data and Hadoop

LocationManager vs GoogleApiClient

Why should I learn Go?