Wednesday 11 November 2020

Part Four: Introduction to Object Oriented Programming (OOP)

 

So far in this course, we have learned a lot of concepts about programming in Python. We have learned about various data types such as strings, integers, floats, Booleans. We have learned about variables and how they are used to store the various data types. We have learned about built-in functions such as print() and input() and how they can be used to manipulate our variables and our data types. We also learned about collections such lists, tuples, sets and dictionaries and how they are used to store more than one variable at a time to be manipulated and processed using our functions. We also learned how to define our own functions, that is user-defined functions where we group our lines of code into a function to be executed.

All these are known as objects which we can display by printing the type. Examples below:

          print(type(5))

          print(type(7.9))

          print(type(True))

          print(type(‘ali’))

print(type([1, 2, 3, 4, 5])

We would the following output

          <class 'int'>

<class 'float'>

<class 'bool'>

<class 'str'>

<class 'list'>

 

As can be seen, the class of each of the objects before is displayed. We have int, float, bool, str, list as the various objects. These are all built-in types (classes)

In Python, it is possible to construct our own user-defined classes (types) also. We do this by starting with the class definition. Then under that, we define the attributes of the class and the methods to be used. This essentially provides us with a template for the class to work with.

Let us consider some practical examples. We could create a class called Person. This Person could have attributes like name and age. This would be our template which we would use to build on. When we run this, then we would have to provide a particular name and the particular age.

Creating the Person class

This is how our code would look like

          class Person:

                   def __init__(self, name, age):

                             self.name = name

                             self.age = age

Let us explain the block of code above. The keyword class is used in Python when we are about to create a class. The class we are creating here is Person (the first letter is capitalised by convention). Next, we have the function with the __init__ method. This is a special method known as an initialiser for the class which tells us which data must be supplied when the instance of class Person is created and how the data is stored and in our case with the class Person, that data is name and age.

There is also the special variable, self, which is always the first parameter passed to the method. We do not provide the data for this parameter, it is left for Python to do but we would supply the name and age ourselves.

So we can now create our Person class with the following codes

          p1 = Person(‘Ali’, 23)

          p2 = Person(‘Jay’, 34)

We have now stored data for 2 people in the Person class with their names and ages, where p1 and p2 have all the attributes of the class Person but are stored in separate locations in memory with different data.

To access an attribute from the Person class, we write the following code

          print(p1.name)

          print(p1.age)

          print(p2.name)

          print(p2.age)

And we have this output

          Ali

23

Jay

34

Or we could even make is more interesting with the code below

          print(p1.name, ‘is’, p1.age, ‘years old’)

          print(p2.name, ‘is’, p2.age, ‘years old’)

And we get the output as follows

          Ali is 36 years old

Jay is 21 years old

We can also update an attribute directly. For example, we could update the age of p1

          p1.age = 38

If we run the code again we get

          print(p1.name, ‘is’, p1.age, ‘years old’)

This is the output we get

          Ali is 38 years old

We could also print the type for Person as well as p1 or p2.

          print(type(Person)

          print(type(p1)

We would get the following output

<class 'type'>

<class '__main__.Person'>

Meaning Person is of class type (object) itself.


Another example coming up.

No comments:

Post a Comment