AlphaCodingSkills

Python Classes and Objects


Advertisements

Previous Page Next Page

Python is an object-oriented programming language and it allows us to create objects. To create an object, first of all, the object should be defined in the program which is done by creating a class. A class is a code template that is used to define a new object type. Within a class, the object's methods and properties are defined and when an object is created, its methods and properties are determined by the class in which it is created.

Create Class and Object

In order to create a class, Python class keyword followed by class name is used and to create an object, class name is used. Please see the syntax below:

Syntax


#defining new object type
class class_name:
      statements

#creating an object
MyObject = cleass_name()

Example:


class circle:
  radius = 10

MyCircle = circle()
print(MyCircle.radius)

Output

10

In above example, a class (new object type) called circle and object of this class called MyCircle are created. MyCircle has only one property radius which is defined in class circle. Please check the syntax for accessing object property inside program.

The __init__() function

The Python __init__() function is a reserved built-in function also called class constructor. It is automatically executed when a new instance of class is created. It allows the class to initialize object's properties or other operations which is needed to create an object.

Example:


class rectangle:
  def __init__(self, length, breadth):
    self.length = length
    self.breadth = breadth

MyRectangle = rectangle(3, 4)
print(MyRectangle.length)
print(MyRectangle.breadth)

Output

3

4

In above example, a new object requires two parameters to define as mention in __init__() method. __init__() method takes these two parameters to initialize necessary object's properties or other operations which is needed to create the object.

Object Methods and Properties

Any variable attribute of an object is called object's property and any function attribute of an object is called object's property.

Example:


class rectangle:
  def __init__(self, length, breadth):
    self.length = length
    self.breadth = breadth
  def area(self):
    return self.length * self.breadth

MyRectangle = rectangle(3, 4)
print(MyRectangle.area())

Output

12

In above example, length and breadth are properties of the object and area() is a method of the object.

The Self Parameter

The self parameter refers to current instance of the class (current object).

Example:


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

MyRectangle_1 = rectangle('rect_1', 3, 4)
MyRectangle_2 = rectangle('rect_2', 2, 5)
MyRectangle_1.compare_area(MyRectangle_2)

Output

Area of  rect_1  > Area of  rect_2

In above example, self refers to current object of class rectangle and other refers to another object of class rectangle. These two objects are taken as parameter in compare_area() method which finally prints comparison result of these two objects.

Modify/Delete Object properties

= operator is used to assign new value to object properties and del keyword is used to delete any object properties.

Delete Object

del keyword is used to delete object.

Example:


class rectangle:
  def __init__(self, length, breadth):
    self.length = length
    self.breadth = breadth
  def area(self):
    return self.length * self.breadth

MyRectangle_1 = rectangle(3, 4)
MyRectangle_2 = rectangle(2, 5)

#modifying length of MyRectangle_1 only
#deleting breadth of MyRectangle_1 only
MyRectangle_1.length = 5
del MyRectangle_1.breadth

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

#delete MyRectangle_2
del MyRectangle_2

Output

10





Previous Page Next Page