Classes and Objects

This is another major concept in Computer Programming. So far, we have studied how to program using functions, that are simply a block of code used to manipulate data. This can be referred to as Procedural Programming. This might sound a bit complicated, but it's not. What is a procedure? Routines or sub-routines. A procedure consists of a number of instructions that need to carried out. As simple as that. We tell the computer what to do, and how to do, with a series of statements. This is what we have been doing so far. Well, this sounds fairly easy, right? What if we have the same task again and again and need to use the same set of functions and data? Observe how we said functions, not instructions here. We use functions to repeat the same instructions again and again, but when it comes to functions and data together, we end up writing the code multiple times.

To tackle this, we use something that can wrap the data and functionalities together, in an object, which is what we call it in computational terms, an object. Hence, we call it Object Oriented Programming. Lets consider a simple example to understand this. We will say that there is a Person called Alex. Alex is the person's name, along with other attributes such as age, date of birth and sex. Alex can do things like talking, walking, eating, and so on. Here, the Class is Person, which consists of a person's information and functionalities. Alex is an object that belongs to the class Person. Alex has attributes like name,age, date of birth, sex,talking, walking, eating, and so on. These are Alex's data and functionalities wrapped together. Hence, an object is the most basic unit of Object Oriented Programming. We can also say that, the objects are instances of the Class, since a Class can have multiple objects. This is where the efficiency kicks in, having one Class that can have multiple objects, which saves us a lot of coding time. You will understand more as we progress forward.

FUN FACT: If you remember, we have studied variables of type int earlier. These variables are actually objects of the Class int.

Let's introduce more formal terms now. Every object can store variables, like name,age, etc in the example above. These are referred to as fields of the Class. The functionalities, such as walking,talking are stored as methods of the Class. Fields and methods, are referred to as the attributes of the Class.

NOTE: Fields are of two types - instance based and class based. The instance variables are the variables of a distinct instance, or object. The class variables are the variables of the Class itself.

The syntax is simple, we use class to initialize a Class and use indented blocks for the attributes.

Lets initiliaze a class now, to put this into practice. You can use the white space on your Repl window and RUN the code at the end to see the result.

class starters:
    def newfunction(self):
        print('hello, there')

s = starters()
s.newfunction()

print(s)

This outputs:

hello, there
<__main__.starters instance at 0x04046EE0>

This is a simple implementation. Let's understand it throughly now. We implemented a class called starters in the code. For now, we started with a simple function that prints 'hello, there'. Then in the next line, we create a new object of the class by using s = starters(), where s is the object. Then, we call the newfunction() from s to print the value. This was a very basic implementation that is helpful in giving us an idea for the units that follow.

The output just shows that we have an instance of starters in the __main__ module. It also gives us the address of the computer memory where the object is stored - 0x04046EE0. This memory address will differ for every implementation as the Python interpretor finds any memory address available to store this.

Note that there is one thing we haven't covered so far, the self statement. It's quite easy to understand this. What do you think it refers to? It simply refers to the instance of the class that called it. In our case, it's s. You will get a much better understanding in the coming units.

results matching ""

    No results matching ""