Example code of Modules Part - 2

Example code of Modules Part - 2

Lesson Details:
June 29, 2020


I: Introduction

The ability to use computers is becoming more and more important in today’s world. Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. It requires high cognitive abilities to solve problems. Anyone who is interested in computer programming should be able to learn it because it can be used in many fields like web development, game development etc.

Python is a general-purpose programming language that was created by Guido Van Rossum in 1991. Python is easy to read and write; it uses English keywords extensively; it has clear, simple syntax; and it has very few syntactic exceptions. Python contains features that are commonly found in other programming languages, but it also contains many powerful features, including object-oriented programming, dynamic access to underlying data structures, and meta-programming capabilities.

Programming languages are designed to communicate instructions to a computer. Both high-level programming languages (such as C or Python) and low-level programming languages (machine code or assembly language) are used to create software applications for computers. High-level programming languages are easier for humans to read and write than machine code, but they are less efficient for computers to execute because each command must be translated from the human-readable form into the machine-readable form before being processed.

In order to have a smooth learning experience, I recommend you use an IDE which will allow you to write your program, compile and run it with one button, fix errors on the fly without going back and forth between the editor and terminal. Also having a good debugger will help you go through errors faster and easier.

II: Body

A: Example code of modules part - 2

To understand how modules work in python, let us first create a simple module. A module is a single file which contains functions which can be used from any other file or from the command line itself. In this chapter we will see how to define a function inside a module and how to call this function from outside the module. In this section we will make a simple module called “math_functions” with some basic mathematical functions like square root, square, cube etc. This way we can use these functions from any file or even from the command line itself without writing any code. Here is a basic example of how a module works:

# math_functions_module_1.py # Square root def square_root(a): print("Square root of",a,"=",math.sqrt(a)) return math.sqrt(a) def square(a): return a*a def cube(a): return a*a*a print("Square root of",3,"=",square_root(3)) print("Square of",3,"=",square(3)) print("Cube of",3,"=",cube(3)) print("-------------------------------") print("Square root of",10,"=",square_root(10)) print("Square of",10,"=",square(10)) print("Cube of",10,"=",cube(10)) print("-------------------------------")

Output:

Square root of 3=1.73205080756888 Square of 3=9 Cube of 3=27 ------------------------------- Square root of 10=3.16227766016838 Square of 10=100 Cube of 10=1000 -------------------------------

Note: When we call square_root() function we pass argument 3 and we get the square root of 3 as output. Similarly when we call square() and cube() functions we pass arguments 3 and 10 respectively and we get the square and cube of those values as output respectively. The global variable ‘math’ refers to the built-in module ‘math’ which provides math related functions like sqrt(), log(), sin() etc. So when you want to access these functions you can just refer them as ‘math’ followed by its function name i.e. ‘math.sin()’ to get sin function value.

Now let us see the same example file written using classes instead of functions:

# math_functions_class_1.py class MathFunctions(): def square_root(self,a): print("Square root of",a,"=",math.sqrt(a)) return math.sqrt(a) def square(self,a): return a*a def cube(self,a): return a*a*a def __init__(self): self.x = 0 def setX(self,x): self.x = x print("Square root of",3,"=",self.square_root(3)) print("Square of",3,"=",self.square(3)) print("Cube of",3,"=",self.cube(3)) print("-------------------------------") print("Square root of",10,"=",self.square_root(10)) print("Square of",10,"=",self.square(10)) print("Cube of",10,"=",self.cube(10)) print("-------------------------------") if __name__ == "__main__": MathFunctions() #calling class directly

Output:

Square root of 3=1.73205080756888 Square of 3=9 Cube of 3=27 ------------------------------- Square root of 10=3.16227766016838 Square of 10=100 Cube of 10=1000 -------------------------------

Note: When we call square_root() function we pass argument 3 and we get the square root of 3 as output. Similarly when you call square() and cube() functions you pass arguments 3 and 10 respectively and you get the square and cube of those values as output respectively. Unlike functions there is no global variable ‘math’ here so each time you want to refer to these functions you have to prefix MathFunctions:: before it like ‘MathFunctions::math’ to access the built-in math module function ‘sqrt()’ inside class MathFunctions(). One advantage is that now whenever you want to change the implementation details of any method/function you just need to change the method implementation inside the class instead of modifying all references in your program wherever they are used then recompiling it again with modified version every time you change implementation details anywhere in your program for that function or method. The reason is that you can access that method/function by referring MathFunctions::nameOfFunction() instead of writing nameOfFunction(). This gives you better organization in your program where methods/functions are grouped together under their respective classes instead of scattered everywhere across your program in different modules or files so they can easily be accessed whenever required by simply referring MathFunctions::nameOfFunction(). Another advantage is that if you want to add more methods inside your class at later time then you only need to add implementation details inside that method inside class definition instead of modifying all references in your program wherever they are used then recompiling it again with modified version every time you add new method implementation details anywhere in your program which decreases development time by reducing recompilation cycles while developing your program which makes it easier for developers to modify any implementation details whenever required without wasting too much time in recompilation cycles while developing their program which makes developers more productive when developing their program at the cost of increased memory usage due to class definitions being stored in memory until GC collects them which is not an issue unless you are working on something where memory consumption becomes an issue due to huge number of classes being defined i.e. software size grows bigger by adding more complexity over time at later stages unlike functions where implementation details are coded at one place unlike classes where implementation details are coded inside class definition so it increases memory consumption slightly while developing your program initially but at later stages when number of classes grow bigger due to complexity added over time instead which means memory consumption remains more or less constant over time unlike functions where memory consumption remains constant throughout its lifetime until GC collects it after its lifetime ends unlike classes where memory consumption increases gradually while adding complexity by adding more classes over time which might not be an issue unless your program grows really huge due to complexity added at later stages unlike functions which are called once per instance unlike classes which are instantiated whenever required which means they occupy memory more than functions due to increase in memory consumption while adding complexity over time unlike functions which are called once per instance whereas classes are instantiated multiple times while calling its methods/functions during run time unlike functions which are compiled once per instance whereas classes are compiled once per file instead while compiling your program which means they occupy memory more than functions due to increase in memory consumption while adding complexity over time unlike functions which can call other functions whenever required whereas classes cannot

loader
Course content