Alias name to a module

Alias name to a module

Lesson Details:
June 29, 2020

I: Introduction

Programming Language is a kind of language that can be used to instruct a computer or a machine on what to do. This language has a syntax and a structure which the programmer is supposed to follow in order for the computer to understand it. There are many programming languages in existence today, and they range from high level languages to low level languages. A high level language usually contains a small instruction set, and a low level language contains a large instruction set. The most commonly used programming language is C++, and some other examples of programming languages include Python, PHP, JavaScript, and HTML. The history of programming languages dates back to 1950’s when the first programming language was created by John McCarthy. The first program written in this language was “the first recursive function”. Today we have many advanced programming languages that we use every day such as Java, C#, etc. This article will discuss an important feature of Python language, and that is alias names to modules.

II: Body

Python is a high level programming language and it is one of the most preferred languages for new programmers. This language is free and open source, and its latest version is named Python 2.7.5. The features of Python make it very flexible and easy to use. It is object oriented, and has dynamic semantics (Madsen n.p.). When you install Python on your system you get more than 100 modules that come with it; these modules let you create applications without having to write everything from scratch (Madsen n.p.). Alias name to a module refers to making changes to the module name, and then using the modified name inside your program. For example: if your module name is “spam”, you can change it to “ham” by using alias name. Aliases are very useful since they allow you to have more control over your code, especially when you are working with someone else on the same project. An example of this is shown below:

import spam #using original name

print(spam) #prints spam

import ham #alias name

print(ham) #prints spam

As you can see above, import spam imports the module called spam, but when you rename it to ham, you can still access it using ham even though it is not using the original name which is spam anymore. Modules are very useful because they allow you to reuse your code whenever you need it again. You can also use modules as subroutines or functions if you want to pass them as arguments to another subroutines or functions. Modules are also very useful if you want to use them later on in your program since they help you maintain good documentation about the source code you are writing. Modules are divided into two categories depending on how they are written or defined; these categories are built-in modules and user written modules (Steinberg 2008). Built-in modules are those that come with the Python interpreter whereas user written modules are those that you create yourself using Python code. Modules can be imported using either an absolute or relative approach depending on how they are written; the absolute approach uses an absolute path whereas the relative approach uses a relative path (Steinberg 2008). For example:

absolute = import sys #uses absolute path

relative = import sys #uses relative path

The above code shows an example of importing sys module using both absolute and relative approaches; sys is an example of a built-in module because it comes with Python interpreter preinstalled. If you want your user written modules to be accessible by another user, then you should use relative paths rather than absolute paths (Linker n.p.). The reason for this is that if you use an absolute path then it will only be available for the person who wrote the module; however if you use a relative path then anyone can access it. The simplest way to use an import statement is to type the name of the module followed by parenthesis (Linker n.p.). For example:

import sys #import sys module

print sys #prints sys module contents

To use sys module further you should assign it to a variable so that you can use it later in your program; for example:

import sys #import sys module

sysvar = sys #assign sys module to a variable called sysvar

print sysvar #prints the contents of the sysvar variable

sysvar() #calls a method from sysvar variable

In order to use aliases when importing a module from another module, then after loading the module from the main file, you add an import statement which will add aliases from the other module (Linker n.p.). For example:

import mod_one #load mod_one module from main file

import mod_two #load mod_two module from main file

from mod_one import * #add aliases from mod_one module to main file scope

from mod_two import * #add aliases from mod_two module to main file scope

Course content