Python Classes

Deepa Mariam George
Analytics Vidhya
Published in
3 min readJun 25, 2020

Object-Oriented Programming

One of the popular approaches to solve a programming problem is by creating objects. This is known as Object-Oriented Programming (OOP).

An object is an identifiable entity and it has two characteristics:

  • attributes
  • behaviour

For instance, we can say ‘Orange’ is an object. Its attributes are: its colour is orange and it is spherical in shape. Its behaviour is: it tastes sweet and it is juicy.

Python is an object-oriented programming language. The concept of OOP in Python focuses on creating reusable code. It follows some basic principles:

Class

A class is a blueprint for creating objects. For example, we can say ‘bird’ is a class and ‘parrot’ is an object of class ‘bird’.

Let’s create a simple class named MyClass. To create a class you can use the keyword class.

class MyClass:
my_name = “Katie”

Object

An object is an instantiation of a class. We create an object of a class using the name of the class followed by a pair of parentheses. Here’s, an example of creating an object for the class MyClass:

class MyClass:
my_name = “Katie”
a = MyClass() //creating an object named 'a'
print(a.my_name)
//accessing my_name using the dot operator with object

The program gives the output:

Katie

The above example implements classes and objects in their simplest form. But these are not really useful in real-life applications.

Constructors in Python

Constructors are generally used for instantiating an object. The task of constructors is to assign values to the variables of a class when an object of the class is created. In Python, the __init__() method is called the constructor. It is always called when an object is created.

Let’s see an example:

class Student:
def __init__(self, name, rollno):
self.name = name
//accessing the attributes with the self keyword
self.rollno = rollno
s1 = Student(“Ali”, 1) //creating an object for the classprint(s1.name)
print(s1.rollno)

The program creates a class named Student and uses the __init__() function to assign values for name and rollno. The output will be:

Ali
1

The self parameter

The self parameter is used to access variables that belong to a class.

Functions defined inside a class must have the self parameter as its first parameter in the function definition. We do not give a value for this parameter when we call the method, but Python provides it.

Methods

Methods are functions defined inside the body of a class. They are used to define the behaviours of an object. Let’s create a method in the Student class.

class Student:
def __init__(self, name, rollno):
self.name = name
//accessing the attributes with the self keyword
self.rollno = rollno
def greeting(self):
print(“Hello ” + self.name + “!”)
s1 = Student(“Ali”, 1)
s1.greeting()

Output:

Hello Ali!

Modifying and Deleting Object Properties

Object properties can be modified or deleted. Here are the examples:

(i) Modifying properties on objects

class Student:
def __init__(self, name, rollno):
self.name = name
self.rollno = rollno
s1 = Student(“Ali”, 1)
s1.name = “Rahul”
print(s1.name)

The value of the attribute name is modified to “Rahul”. So the output becomes:

Rahul

(ii) Deleting properties on the object

Properties on objects can be deleted using the del keyword.

class Student:
def __init__(self, name, rollno):
self.name = name
self.rollno = rollno
s1 = Student(“Ali”, 1)del s1.rollno //attribute rollno gets deleted

(iii) Deleting objects

Using the del keyword an object itself can be deleted.

class Student:
def __init__(self, name, rollno):
self.name = name
self.rollno = rollno
s1 = Student(“Ali”, 1)del s1 //object s1 gets deleted

--

--