Modules:
Modules enable you to split parts of your program in
different files for easier maintenance and better performance.
As a beginner, you start working with Python on the interpreter, later when you need to write longer programs you start writing scripts. As your program grows more in the size you may want to split it into several files for easier maintenance as well as reusability of the code. The solution to this is Modules. You can define your most used functions in a module and import it, instead of copying their definitions into different programs. A module can be imported by another program to make use of its functionality. This is how you can use the Python standard library as well.
The import statement:
To use the functionality present in any module, you have to import it into your current program. You need to use the import keyword along with the desired module name. When interpreter comes across an import statement, it imports the module to your current program. You can use the functions inside a module by using a dot(.) operator along with the module name. First, let's see how to use the standard library modules. In the example below,math module is imported into the program so that you can use sqrt() function defined in it.
import
math #You need to put this
command,`import` keyword along with the name of the module you want to import
num
= 4
print(math.sqrt(num)) #Use dot operator to access sqrt() inside
module "math"
Output:
2
from .. import statement”
The from..import statement allows you to import specific
functions/variables from a module instead of importing everything. In the
previous example, when you imported calculation into module_test.py, both the
add() and sub() functions were imported. But what if you only needed the add()
function in your code?
Here is an example to illustrate the use of from..import
from
calculation import add
print(add(1,2))
In above example, only the add() function is imported and
used. Notice the use of add()? You can now access it directly without using the
module name. You can import multiple attributes as well, separating them with a
comma in the import statement. Take a look at the following example:
from
calculation import add,sub
from .. import * statement:
You can import all attributes of
a module using this statement. This will make all attributes of imported module
visible in your code.
Here is an example to illustrate
the use of from .. import *:
from
calculation import *
print(add(1,2))
print(sub(3,2))
Note that in the professional
world, you should avoid using from..import and from..import*, as it makes your
code less readable.
Renaming the imported module:
You can rename the module you are
importing, which can be useful in cases when you want to give a more meaningful
name to the module or the module name is too large to use repeatedly. You can
use the as keyword to rename it. The following example explains how to use it
in your program.
import
calculation as cal
print(cal.add(1,2))
You saved yourself some typing
time by renaming calculation as cal.
Note that you now can't use
calculation.add(1,2) anymore, as calculation is no longer recognized in your
program.
Python - Sys Module:
The sys module provides functions and variables used to
manipulate different parts of the Python runtime environment. You will learn
some of the important features of this module here.
sys.argv:
sys.argv returns a list of command line arguments passed to
a Python script. The item at index 0 in this list is always the name of the
script. The rest of the arguments are stored at the subsequent indices.
Here is a Python script (test.py) consuming two arguments
from the command line.
Example: test.py Copy
import
sys
print("Hello
{}. Welcome to {} tutorial".format(sys.argv[1], sys.argv[2]))
This script is executed from command line as follows:
C:\python36>
python test.py Martin Python Hello Martin. Welcome to Python tutorial
As you can see, sys.argv[1] contains the first parameter
'Martin', while sys.argv[2] contains the second parameter 'Python'. sys.argv[0]
contains the script file name test.py.
sys.exit:
This causes the script to exit back to either the Python
console or the command prompt. This is generally used to safely exit from the
program in case of generation of an exception.
sys.maxsize
Returns the largest integer a variable can take.
>>> import sys
>>>sys.maxsize
9223372036854775807
sys.path
This is an environment variable that is a search path for
all Python modules.
>>>sys.path
['', 'C:\\python36\\Lib\\idlelib',
'C:\\python36\\python36.zip', 'C:\\python36\\DLLs', 'C:\\python36\\lib',
'C:\\python36', 'C:\\Users\\acer\\AppData\\Roaming\\Python\\Python36\\site-packages',
'C:\\python36\\lib\\site-packages']
sys.version
This attribute displays a string containing the version
number of the current Python interpreter.
>>>sys.version
'3.7.0 (v3.7.0:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]'
Comments
Post a Comment