Python - Classes and Objects
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. To create an object, class name is used. See the syntax below:
Syntax
#defining new object type class className: statements #creating an object objectName = className()
Example:
In the example below, 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.
class circle: radius = 10 MyCircle = circle() print(MyCircle.radius)
The output of the above code will be:
10
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:
In the example below, 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.
class rectangle: def __init__(self, length, breadth): self.length = length self.breadth = breadth MyRect = rectangle(3, 4) print(MyRect.length) print(MyRect.breadth)
The output of the above code will be:
3 4
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 method.
Example:
In above example, length and breadth are properties of the object and area() is a method of the object.
class rectangle: def __init__(self, length, breadth): self.length = length self.breadth = breadth def area(self): return self.length * self.breadth MyRect = rectangle(3, 4) print(MyRect.area())
The output of the above code will be:
12
The Self Parameter
The self parameter refers to current instance of the class (current object).
Example:
The example below shows how to use self parameter in a python code.
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) Rect1 = rectangle("Rect1", 3, 4) Rect2 = rectangle("Rect2", 2, 5) Rect1.compare_area(Rect2)
The output of the above code will be:
Area of Rect1 > Area of Rect2
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 Rect1 = rectangle(3, 4) Rect2 = rectangle(2, 5) #modifying length of Rect1 only #deleting breadth of Rect1 only Rect1.length = 5 del Rect1.breadth #Rect2 or class definition will not change print(Rect2.area()) #delete Rect2 del Rect2
The output of the above code will be:
10