Class and Object in Python

Class and Object in Python

Class and object in Python are possible because it is an object-oriented programming language. In order to create an object, it should be defined first which is done by creating a blue-print also known as the class. A class is a template which specifies what properties and behaviors which correspond to data members and methods, its objects will have.

 

 

Create Class and Object in Python

In Python, a class is created using the class keyword which is placed before the name of the class indicating that the variable is a class. An object is created by using the class name. Syntax of class and object creation is given below:

Create Class and Object syntax:

#defining a class which is a new object type
class class_name:
      statements

#creating an object
object_name = class_name()

Create Class and Object example:

class circle:
  radi = 7

objCircle = circle()
print(objCircle.radi)

Output:

7

 

 

The __init__() Function

The __init__() function available in Python is a reserved built-in function which is nothing but a constructor of the class in which it is defined. Whenever an instance of a class is created, it is called automatically. It’s main purpose is to initialize object’s properties or perform some basic operations which are required upon object creation.

__init__() function example:

# Python program to illustrate __init__() function

class rectangle:
  def __init__(self, len, br):
    self.len = len
    self.br = br

Rect = rectangle(7, 2)
print(Rect.len)
print(Rect.br)

Output:

7
2

 

 

Object’s Methods and Properties

Any function defined inside a class is the method of the objects created of that class and any variable defined inside a class is the property of the objects created of that class.

Object’s methods and properties example:

# Python program to illustrate object's properties and methods

class rectangle:
  def __init__(self, len, br):
    self.len = len
    self.br = br

  def area(self):
    return self.len * self.br

Rect = rectangle(7, 2)
print(Rect.area())

Output:

14

 

 

The self Parameter

In Python, the self parameter refers to the current instance or the current object of the class.

The self Parameter example:

# Python program to illustrate the self parameter

class rectangle:
  def __init__(self, title, len, br):
    self.title = title
    self.len = len
    self.br = br
  def area(self):
    return self.len * self.br
  def compare_area(self, other):
    if self.area() > other.area():
      print("Area of ",self.title," > Area of ",other.title)
    elif self.area() < other.area():
      print("Area of ",self.title," < Area of ",other.title)
    else:
      print("Area of ",self.title," = Area of ",other.title)

Rect1 = rectangle("Rectangle1", 2, 4)
Rect2 = rectangle("Rectangle2", 5, 5)
Rect1.compare_area(Rect2)

Output:

Area of Recatangle1 is < Area of Rectangle2

 

 

Modify/Delete Object Properties

To modify a property of an object, = operator is used to assign the new value to it and to delete any property of an object, del keyword is used. del keyword is also used to delete the object.

Modify/Delete Object Properties example:

class rectangle:
  def __init__(self, len, bre):
    self.len = len
    self.br = br
  def area(self):
    return self.len * self.br

Rect1 = rectangle(2, 4)
Rect2 = rectangle(4, 5)

#modifying len of Rect1 only
#deleting br of Rect1 only
Rect1.len = 5
del Rect1.br

#Rect2 or class definition will not change
print(Rect2.area())

#delete Rect2
del Rect2

Output:

20

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *