Till now we have been writing code around functions with certain input, written an algo to manipulate them, and return something. Whether it would be search algorithm or a function to test something or scripts which manipulate some external resources based on the given input. This style of writing code is called "Procedural way of programming".This is good practice but not ideal for larger project. At this point we use another style of programming called "Object Oriented way of Programming".
In this way of programming, we break down a task into objects, which uses some defined methods and data. It is a model organized around "class/object" rather than functions. Objects are modeled after real-world objects.
So Object Oriented Programming is a programming paradigm which is organized around object (involving both data and methods) and its aim to take advantage of code re-usability.
Class is a logical grouping of data and functions (referred as "methods" when defined within a class). Classes are created after some real-word objects, concept like customer, product or urllib. So rather than just putting random things together under the name "class", we try to create classes where there is a logical connection between things.
Classes can be thought as blueprint of creating object. When we make a blueprint of a building, we aren't actually making a building; we are defining its various properties like number of room, basic layout, and functionality. When our blueprint is ready then we can make any number of buildings based on that blueprint, add additional function, and change defined function.
Defining a sample class in Python
class Class_name(object): """ docstring for class_name """ pass
Now let's take an example. Suppose we have to write some code which store basic information about the student of a certain school.
class Student(object): """ A base class of student for school xyz """ total_student =0 def __init__(self,name,standard,average_grade): self.name = name self.standard= standard self.average_grade= average_grade Student.total_student += 1 def display_number_of_student(self): print "Total Number of Student: %d" %Student.total_student def display_info(self): print "Name:", self.name, ", Standard: ", self.standard, ", Average Grade:", self.average_grade
To create an object [Instance] from class, we will call the class in the same way as function with the arguments (minus self) given to
smith= Student("Jon Smith",8,'B') kumar= Student("Rahul Kumar",9,'C')
Oh, Things got too hasty, don’t Worry. We will get on everything step by step.
This would be first method in a class and it is called "initialize" or "constructor". The first argument to
init would be self(we will get on this). Along with self, it takes additional argument, which we are going to use and manipulate in the class.These argument is called attributes .
A good practice is to define all the attributes inside the
So what is
self ? Along with
init , every method in student class take self as their argument.
To create our first instance(object) smith, we have called the class along with some arguments which go to
init function. When we say smith is the instance, then self refer to smith.
So When we create a Instance, instead of using
smith= Student("smith", "Jon smith", 8,'B')
smith= Student("Jon smith", 8,'B').
Python automatically interrupt self as smith. We don't have to additionally explain this.
Now why is
self present in other method?
When we access any method of class student using smith then every method interrupt self as smith and process the information regarding that instance. Methods are called using
instance_name.method_name(argus) so here We haven't define the what is self. Here instance_name is taken as self. an another way to define this would be
Ex. When we call
smith.display_info() the output would be
Name: Jon Smith , Standard: 8 , Average Grade: B but when we call
kumar.display_info() the output would be
Name: Rahul Kumar , Standard: 9 , Average Grade: C
When we create an Instance,
init is called and a fully initialized object is ready.
Now with this object we can access all the methods defined inside the class.
While reading above, you have encountered a lot of new words, which are not making sense if you're new to Object Oriented Programming. Now this is perfect time to define them. Let's start:
Simply a logical grouping of data and functions, a blueprint and a building block of python.
Example of class- Here Student is a class which is a blueprint of student of a certain school
When we call the class, we create a Instance of that class. We can create as many instance as we want for a particular class.
smith and kumar are two instances of class Student.
Functions defined inside the class are called Methods. Sometime theses are also called "Instance Methods" as these methods take self as their first argument(require an instance to operate).
Example : function
display_number_of_student() and display_info() are called methods.
The different variable defined inside the
init function are called attributes.
Name, Standard and average_grade are attributes of class Student.
Variables that are defined inside a Instance Method and only belong to that particular Instance Method are called "Instance Variable".
Class variable is a variable which is shared by all instance of a class. Class variable can be accessed from inside or outside of the class. Class variables are defined within a class but outside any of the class's methods.
variable total_student in class Student is a class variable.
We know that class variable is access-able from inside or outside the class.
print smith.total_student print kumar.total_student
If we try to access an attribute which is not defined, then python will raise an
We want to create a student log for certain school. To do so we have first prepared a blueprint (i.e created a class called Student). Till now we haven't create log for any student. We just outlined information and function.
Now we create our first instance by giving proper argument to Student class [
smith= Student("Jon Smith",8,'B')]. Here we are telling python that use Student Blueprint to make a student log and we will refer this as smith.
Again when we call
kumar= Student("Rahul Kuamr",9,"C") then we told python to use Student blueprint to create a student log and we will refer this as kumar.
smith and kumar are known as Instance of Class Student. We can create as many Instances as we want. These Instance have access to Instance methods and class variables.
Thus far, we have gained the basic knowledge of Class and why they are useful and how to use them(hopefully).Object Oriented is a wide topic.This post is not meant a one-stop for classes.Read more about Classes to get a deep insight.
And Corrections and suggestions are welcome in the comments.Published On : 2017-02-04 Tweet