Classes and objects
around us and in Python
Sukrit Gupta
October 19, 2023
Sukrit Gupta Intro to Computing and Data Structures 1/24
Outline
1 Different Programming Paradigms
2 Understanding classes
3 Classes and objects
Sukrit Gupta Intro to Computing and Data Structures 2/24
Acknowledgement and disclaimer
All mistakes (if any) are mine.
I have used several other sources which I have referred to in the
appropriate places.
Sukrit Gupta Intro to Computing and Data Structures 3/24
Section 1
Different Programming Paradigms
Sukrit Gupta Intro to Computing and Data Structures 4/24
What are programming paradigms?
Programming paradigms are different ways or styles in which a given
program or programming language can be organized.
Each paradigm consists of certain structures, features, and opinions
about how common programming problems should be tackled.
Programming paradigms are not languages or tools. You can’t “build”
anything with a paradigm. They’re more like a set of ideals and
guidelines that many people have agreed on, followed, and expanded
upon.
Certain paradigms are better suited for certain types of problems, so it
makes sense to use different paradigms for different kinds of projects.
Sukrit Gupta Intro to Computing and Data Structures 5/24
Programming languages aren’t always tied to a specific paradigm.
Programming languages have evolved over time to pick up features
that help problem-solving from different paradigms. For example:
We started with giving detailed step-by-step instructions to the
computer that we wanted to be executed in a given order
(Imperative Programming).
Then we added procedures (functions) to it, in order to improve the
modularity of the code and organize the code better (Procedural
Programming).
To prevent reinventing the wheel everytime we sit down to code, we
brought in abstraction whereby the user can reuse existing code and
its underlying complexity is hidden from the user (Declarative
Programming).
Then came this idea of creating separate entities (objects) with
their own set of information and their own set of operations that
can be performed on them (Object Oriented Programming).
There are many programming paradigms, but we will focus on the
Object Oriented Programming paradigm.
Sukrit Gupta Intro to Computing and Data Structures 6/24
Object Oriented Programming
Object-Oriented Programming (OOP) programming paradigm
relies on the concept of classes and objects.
It is used to structure a software program into simple, reusable
pieces of code blueprints, called classes, which are used to create
individual instances of objects. OOP languages examples:
JavaScript, C++, Java, and Python.
OOP languages are not necessarily restricted to the OOP
paradigm. Python, for example, allows for both procedural and
object-oriented programming styles.
Sukrit Gupta Intro to Computing and Data Structures 7/24
Section 2
Understanding classes
Sukrit Gupta Intro to Computing and Data Structures 8/24
Students on the IIT Ropar campus
All the students on the IIT Ropar campus have some features that
distinguish them from other students.
These can be termed as data attributes. Some common attributes?
Let’s say: Name, Age, Year of Joining.
Now, let’s assume that you don’t know anything about
classes/objects.
How will you model the data for each student with your present
knowledge?
Sukrit Gupta Intro to Computing and Data Structures 9/24
Modeling students on the IIT Ropar campus
student_1 = {'name': 'Yatri', 'age': 19, 'start_year': 2023}
student_2 = {'name': 'Lakshay', 'age': 19, 'start_year': 2023}
student_3 = {'name': 'Arpit', 'age': 19, 'start_year': 2023}
Something like the above?
There is so much repetition in the above!
Sukrit Gupta Intro to Computing and Data Structures 10/24
Thinking about classes and objects
1
Figure: Assume that this is a faceless human.
Close your eyes.
Visualize the same figure you just saw. It has no face, no name, no
distinct characteristics of its own.
If I put a 100 such figures in front of you, you cannot differentiate them
from each other.
1
Credits: pngwing.com/en/free-png-pmsve
Sukrit Gupta Intro to Computing and Data Structures 11/24
However, each of the figures can have characteristics if I assign them to
it. It can have a name (the person it belonged to), an age, a gender,
and so on ...
Figure: An instance of the human figure class.
The figure in itself is a class. It represents the set of characteristics a
person can/could possess. Each person is an object of the figure class.
Sukrit Gupta Intro to Computing and Data Structures 12/24
Section 3
Classes and objects
Sukrit Gupta Intro to Computing and Data Structures 13/24
Syntax for a class
Each class definition begins with the reserved word class followed by
the name of the class and some information about how it relates to
other classes.
class class_name:
def __init__(self, arguments): #constructor function
body of constructor
Following questions need to be answered:
Where is the object?
What is the init () function?
What is the self argument to the init () function?
We will understand these with an example. Let’s see.
Sukrit Gupta Intro to Computing and Data Structures 14/24
Let’s look at an example.
class figure:
def __init__(self, name_, age_, gender_):
self.name = name_
self.age = age_
self.gender = gender_
def print_info(self):
print('Name: ', self.name,
'Age:', self.age,
'Gender:', self.gender)
S1 = figure('Sukrit', 99, 'M') #Object: S1
S1.print_info()
S2 = figure('Swapnil', 89, 'F') #Object: S2
S2.print_info()
Sukrit Gupta Intro to Computing and Data Structures 15/24
Constructor function
Python has a number of special method names that start and end
with two underscores.
The task of constructors is to initialize the data members of the
class when an object of the class is created.
In Python the init () method is called the constructor and is
always called when an object is created.
When instantiating a class we need to look at the specification of
the init () function for that class to know what arguments to
supply and what properties those arguments should have.
Always returns None.
Sukrit Gupta Intro to Computing and Data Structures 16/24
The self argument
The self parameter is a reference to the current instance of the
class.
It is used to access variables that belong to the class. It has to be
the first parameter of any function in the class.
It does not have to be named self, can be given a random name.
class figure():
def __init__(abc, name, age, gender):
abc.name = name
abc.age = age
abc.gender = gender
def print_info(abc):
print('Name: ', abc.name,
'Age:', abc.age,
'Gender:', abc.gender)
S1 = figure('Sukrit', 99, 'M') #Object: S1
S1.print_info()
Sukrit Gupta Intro to Computing and Data Structures 17/24
An example with cars
All cars have some common characteristics. For example: each car has
a color, maximum speed, number of seats, registration number, et
cetera
Let’s write a program that can create a skeleton for the car and
instantiate it.
Sukrit Gupta Intro to Computing and Data Structures 18/24
By the way, ...
l = list([1, 2, 3])
What do you see here?
Since everything is an object in Python, data types are actually classes
and variables are instance (object) of these classes.
Sukrit Gupta Intro to Computing and Data Structures 19/24
What is this?
l = list([1, 2, 3])
What do you see here in terms of classes/objects?
Ultimately the list class hides away the details of what is going in the
background for the object l.
Sukrit Gupta Intro to Computing and Data Structures 20/24
This is a very powerful concept. The object l can now invoke several
methods that are defined in the list class.
However, the user doesn’t need to know the details of how these
methods are implemented. The user just needs to know how to call a
particular method he wants to use.
Sukrit Gupta Intro to Computing and Data Structures 21/24
a = 5
print("Type of a: ", type(a))
b = 5.0
print("Type of b: ", type(b))
Sukrit Gupta Intro to Computing and Data Structures 22/24
What did we learn today?
1 Different Programming Paradigms
2 Understanding classes
3 Classes and objects
Sukrit Gupta Intro to Computing and Data Structures 23/24
Thank you!
Sukrit Gupta Intro to Computing and Data Structures 24/24