Object-Oriented Programming (OOP) is a programming paradigm. OOP treats objects as the basic units of a program, where each object contains data and functions that operate on that data.
Procedural programming views a computer program as a collection of commands, i.e., a sequential execution of a set of functions. To simplify programming, procedural programming further splits functions into subfunctions — reducing system complexity by breaking large functions into smaller ones.
In contrast, object-oriented programming views a computer program as a collection of objects. Each object can receive and process messages sent by other objects, and the execution of a computer program is a series of messages passed between these objects.
In Python, all data types can be treated as objects, and you can also define custom objects. A custom object data type corresponds to the concept of a Class in object-oriented programming.
Let’s use an example to illustrate the difference in program flow between procedural and object-oriented approaches.
Suppose we need to process a student grade sheet. To represent a student’s grades, a procedural program might use a dict:
std1 = { 'name': 'Michael', 'score': 98 }
std2 = { 'name': 'Bob', 'score': 81 }
Processing student grades can be implemented via functions, such as printing a student’s grade:
def print_score(std):
print('%s: %s' % (std['name'], std['score']))
If we adopt an object-oriented programming mindset, our first consideration is not the program’s execution flow, but that the Student data type should be treated as an object with two properties: name and score. To print a student’s grade, we first create an object corresponding to that student, then send a print_score message to the object, allowing it to print its own data.
class Student(object):
def __init__(self, name, score):
self.name = name
self.score = score
def print_score(self):
print('%s: %s' % (self.name, self.score))
Sending a message to an object is essentially calling the associated function of that object, which we refer to as the object’s Method. An object-oriented program would be written like this:
bart = Student('Bart Simpson', 59)
lisa = Student('Lisa Simpson', 87)
bart.print_score()
lisa.print_score()
The object-oriented design philosophy is derived from nature, where the concepts of Class and Instance are intuitive. A Class is an abstract concept — for example, the Student Class we defined refers to the concept of a “student” — while an Instance is a specific realization of that Class (e.g., Bart Simpson and Lisa Simpson are two specific Student instances).
Thus, the core of object-oriented design is to abstract a Class, then create Instances based on that Class.
Object-oriented programming is more abstract than functions, as a single Class contains both data and methods to operate on that data.
Encapsulation, inheritance, and polymorphism are the three core features of object-oriented programming — we will elaborate on these in detail later.