python
  1. python-object-class

Python Object Class

Python is an object-oriented programming language. Every entity in Python is an object, and each object has a specific class to which it belongs. A class is a blueprint that defines the attributes and behavior of an object.

Syntax

The syntax to define a class in Python is as follows:

class ClassName:
    <class variables>
    
    def __init__(self, <parameters>):
        <instance variables>
    
    def <method_name>(self, <parameters>):
        <method_body>
  • class ClassName:: This is the class definition line where class name is specified.
  • <class variables>: These are the variables that are shared by all the objects of the class.
  • def __init__(self, <parameters>):: The "init" method (constructor) is a special method in Python that gets called when an object of a class is created.
  • <instance variables>: These are the variables that are specific to each object of the class.
  • def <method_name>(self, <parameters>):: Methods are the behavior or action that an object can perform.
  • <method_body>: This is the implementation details of the method.

Example

Let's create an example to understand the object class:

class Person:
    count = 0
    
    def __init__(self, name, age):
        self.name = name
        self.age = age
        Person.count += 1
        
    def display(self):
        print("Name:", self.name)
        print("Age:", self.age)
      
    def __del__(self):
        print(self.name, "is deleted")
      
p1 = Person("John", 30)
p2 = Person("Smith", 25)

p1.display()
p2.display()

print("Total number of persons:", Person.count)

del p1

Output:

Name: John
Age: 30
Name: Smith
Age: 25
Total number of persons: 2
John is deleted

Explanation

In the above example, we have created a class "Person" that has three instance variables: name, age, and count. count is a class variable that counts the number of persons created. The constructor method __init__ initializes the instance variables name and age.

We have also defined a method display to display the name and age of the person. The __del__ method is a destructor method that gets called when an object of the class is deleted.

We have created two objects p1 and p2 of the class Person and called the display method to display their names and ages. Then we have displayed the total number of persons created. Lastly, we have deleted the p1 object which triggers the __del__ method.

Use

  • Classes are used to create objects that have specific attributes and methods.
  • They help in organizing the code in a modular and reusable way.
  • Python standard library has many built-in classes like list, tuple, set, etc.

Important Points

  • All the properties and behavior of an object are defined by the class it belongs to.
  • Every object has a specific class to which it belongs.
  • Classes are used to create objects with specific attributes and methods.
  • The __init__ method is a constructor method that gets called when an object of the class is created.
  • The __del__ method is a destructor method that gets called when an object of the class is deleted.

Summary

In this tutorial, we learned about the Python object class, which is a blueprint that defines the attributes and behavior of an object. We learned about the syntax to define a class in Python and how to create objects of that class. We also learned about class variables, instance variables, methods, constructor, and destructor methods, and their usage in Python.

Published on: