Object Oriented Programming within Python

 

More about Functions in Python:

As was mentioned towards the beginning of the previous chapter, functions are basically a collection of commands and functions that get executed in sequential order during run time. All the functions you write must be assigned an acceptable name and may require parameters.

Use:

Functions are made up of two main sections:

  1. A header that begins with a keyword (def:), has a name, a set of brackets that may contain a lost of parameters and then a colon. (see Fig. 9 on p. 26)
  2. A body that contains the statements to be run when the function is called. The way in which the statements are encapsulated within a function (a block of code) is by indenting the code 4 spaces which is a Python standard.

Once you have coded a set of function they are ready to be run or called as it is called in programming. Calling a function is done by typing its name followed by a set of parenthesise. If there are any parameters required to run a function they will be listed in within these parenthesise. Please remember that Python is a case sensitive programming language and so you must be sure to get the function’s name exactly right.

There are situations where functions require no parameters and in these cases you still require the parenthesise even though they contain nothing. Python does look for the presence of these parenthesise to distinguish between variables and functions.

Flow of Execution:

Flow of execution refers to the order in which the code you enter into your programs gets run. One of the first higher level programming languages is Basic. Basic is strictly a sequential programming language that gets executed according to line number. The only choice a programmer has to ‘jump’ around within the program is to use a statement called ‘goto’ and that then allows the programmer to skip to another line number.

Apart from the fact that this is a very simple flow of execution there ended up being a lot of drawbacks to this approach. This led to the development of and introduction to Object Oriented Programming (OOP). Python is an OOP programming language.

The first thing that a Python compiler looks for is the call for the main function. The compiler will then start executing the code within that main function. As with Basic each line then gets executed from the top down.

It is possible to have various other functions called from within any function. When this happens, execution will be transferred to the function that was called and if the result to an algorithm was required it will be returned to the calling function and the flow of execution continues.

It must be understood that a function can carry out a large variety of tasks – from doing a simple calculation to establishing a connection to a database hosted on another networked computer / server. One of the advantages of functions is that they are blocks of code that can be called at any time from anywhere within the program effectively allowing you to re-use code as is required.

In most cases functions require arguments / parameters to run, for example the built in function, ‘max(num1, num2)’ requires two numeric parameters and will return the biggest of the two numbers.

At this point I would like to draw your attention back to the example given on p. 26 Fig. 9. In this example there are three functions:

  1. The main() function.
  2. The getText() function.
  3. The sayHello() function.

In line 15 of the program there is a statement that calls the main() function. That is the first execution that the Python Compiler / Interpreter will perform. The only operation that takes place in the main() function is that the getText() function is called. The getText() function actually does something in that it creates a variable in memory and assigns it some string data. The next thing that happens in the getText() function is that the sayHello() function is called.

The sayHello() function requires a parameter to run and so the variable (message) that was declared and instantiated in the getText() function gets sent to it. In the sayHello() environment the ‘message’ variable is known as ‘stuff’ which is actually merely a pointer to the variable ‘message’. All what this function then does is it outputs the string to the screen (output device) using the ‘print’ keyword.

The interactive Python shell provides us with a convenient way to test our functions. We can use the import statement to bring the functions we have defined in a script into the interpreter session. To see how this works, we will develop a similar program to the one on p. 26 with one small difference: In this program we are going to allow the user to enter the message to be displayed: Start a new program in Notepad++ and save it as ‘HelloWorldV3.py’.

49
Fig.49

Assuming that your Python programs / modules can be found by the interpreter via the search path, this is how you can access Python functions using the import statement: Within the Python shell, type in the following:

50Fig.50

In the first line the whole program was imported and run giving the expected output. After that just the getText() function was called which led to the sayHello() function outputting the new text (Bzzzzzz). This should give you a good understanding of the order of execution that takes place in Python programs.

Since Computer Science and Mathematics are closely related the modes operands for functions in Python are similar to Mathematical functions. One of these similarities is that functions in Python can be composed like in Mathematics. What this means is that the results of one function may be used as parameters / arguments (input) for another function.

In the following example the message to be output is going to be the product of five multiplied by itself five times (55). In python that is expressed in the following way: 5**5. The result of that algorithm will then be set to the sayHello() function and output to the screen.

51
Fig.51

Another example of composition is where you use a variable as a parameter / argument for a function. In the following example we are going to declare and instantiate a string variable and use it as the parameter for the sayHello() function:

52
Fig.52

At this stage I would like to draw your attention to the fact that variables and parameters are local within functions. This means that they cannot be used outside the functions unless they are actually passed to other functions. In the above example the parameter (et) gets referenced through the variable ‘stuff’ using the (et) variable‘s location in memory.

pl

« Previous || Next »