Class

Since we are going to save student information, the class is going to be called Student. A class is defined using the class keyword as follows:

class Student(object):

Thus, a class called Student has been defined. Whenever a new object is created, the method __init__() (the underscore indicate that the init method is a magic method, that is it is a function that is called by Python when an object is created) is called internally by Python.

This method is defined within the class:

class Student(object): 
"""A Python class to store student information"""

def __init__(self, name, address, age):
self.name = name
self.address = address
self.age = age

In this example, the arguments to the __init__ method include name, age and address. These arguments are called attributes. These attributes enable creating a unique object that belongs to the Student class. Hence, in this example, while creating an instance of the Student class, the attributes name, age, and address are required arguments.

Let's create an object (also called an instance) belonging to the Student class:

student1 = Student("John Doe", "123 Main Street, Newark, CA", "29")

In this example, we created an object belonging to the Student class called student1 where John Doe (name), 29 (age) and 123 Main Street, Newark, CA(address) are attributes required to create an object. When we create an object that belongs to the Student class by passing the requisite arguments (declared earlier in the __init__() method of the Student class), the __init__() method is automatically called to initialize the object. Upon initialization, the information related to student1 is stored under the object student1.

Now, the information belonging to student1 could be retrieved as follows:

print(student1.name) 
print(student1.age)
print(student1.address)

Now, let's create another object called student2:

student2 = Student("Jane Doe", "123 Main Street, San Jose, CA", "27")

We created two objects called student1 and student2. Each object's attributes are accessible as student1.name, student2.name and so on. In the absence of object oriented programming, we will have to create variables like student1_name, student1_age, student1_address, student2_name, student2_age and student2_address and so on. Thus, OOP enables modularizing the code.