../_images/book_cover.jpg

This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods.

The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!

< CHAPTER 7. Object Oriented Programming (OOP) | Contents | 7.2 Class and Object >

Introduction to OOP

So far, all the codes we have written belong to the category of procedure-oriented programming (POP), which consists of a list of instructions to tell the computer what to do; these instructions are then organized into functions. The program is divided into a collection of variables, data structures, and routines to accomplish different tasks. Python is a multi-paradigm programming language, which means it supports different programming approach. One different way to program in Python is object-oriented programming (OOP). The learning curve is steeper, but it is extremely very powerful and worth the time invested in mastering it. Note: you do not have to use OOP when programming in Python. You can still write very powerful programs using the POP. That said, the POP is good for simple and small programs, while the OOP is better suited for large programs. Let us take a closer look at object-oriented programming.

The object-oriented programming breaks the programming task into objects, which combine data (known as attributes) and behaviors/functions (known as methods). Therefore, there are two main components of the OOP: class and object.

The class is a blueprint to define a logical grouping of data and functions. It provides a way to create data structures that model real-world entities. For example, we can create a people class that contains the data such as name, age, and some behavior functions to print out ages and genders of a group of people. While class is the blueprint, an object is an instance of the class with actual values. For example, a person named ‘Iron man’ with age 35. Put it another way, a class is like a template to define the needed information, and an object is one specific copy that filled in the template. Also, objects instantiated from the same class are independent from each other. For example, if we have another person - ‘Batman’ with age 33, it can be instantiated from the people class, but it is an independent instance.

Let us implement the above example in Python. Do not worry if you do not understand the syntax below; the next section provide more helpful examples.

class People():
    def __init__(self, name, age):
        self.name = name
        self.age = age
        
    def greet(self):
        print("Greetings, " + self.name)
person1 = People(name = 'Iron Man', age = 35)
person1.greet()
print(person1.name)
print(person1.age)
Greetings, Iron Man
Iron Man
35
person2 = People(name = 'Batman', age = 33)
person2.greet()
print(person2.name)
print(person2.age)
Greetings, Batman
Batman
33

In the above code example, we first defined a class - People, with name and age as the data, and a method greet. We then initialized an object - person1 with the specific name and age. We can clearly see that the class defines the whole structure, while the object is just an instance of the class. Later, we instantiated another object, person2. It is clear that person1 and person2 are independent with each other, though they are all instantiated from the same class.

The concept of OOP is to create reusable code. There are three key principles of using OOP:

  • Inheritance - a way of creating new classes from existing class without modifying it.

  • Encapsulation - a way of hiding some of the private details of a class from other objects.

  • Polymorphism - a way of using common operation in different ways for different data input.

With the above principles, there are many benefits of using OOP: It provides a clear modular structure for programs that enhances code re-usability. It provides a simple way to solve complex problems. It helps define more abstract data types to model real-world scenarios. It hides implementation details, leaving a clearly defined interface. It combines data and operations.

There are also other advantages to use OOP in a large project. We encourage you can search online to find out more. At this point, you may still not fully understand OOP’s advantages until you are involved in complex large projects. We will continue to learn more about OOP during the course of this book, and its usefulness will become apparent.

< CHAPTER 7. Object Oriented Programming (OOP) | Contents | 7.2 Class and Object >