Classes and Objects in Python With Examples

Python class and object are the main part of object-oriented programming language. A collection of objects is known as a class in the object-oriented programming language. A class is nothing but a blueprint of an object that describes the common attributes and behavior. Objects are nothing but an instance of a class. Using an object, programmers can access the private data and method of a class. The object is an entity and it also has state and behavior. It represents the real-world objects like the keyboard, mouse, chair, table, pen, etc. In an object-oriented programming language, everything is an object.

Creation of Python Classes:

Some key point about Python class:

  • The class keyword is used to create classes in Python language.
  • Here, the variables are nothing but the attributes that belong to the class.
  • These attributes that are belonging to the class can be accessed using the dot operator.
  • These attributes are always representing as public access specifiers.

Syntax:

# The class is created using class keyword
class Class_Name:
      block of statement

Creation of Python Objects:

The objects in object-oriented programming language represent an instance of a class. The objects can be created by using the class name. The class also can be called by using the class name. An object has three characteristics and they are described below:

State: The attributes are used to represent the state of an object. The properties of the object are also reflected by using it.

Behavior: The class methods are used to represent the behavior of the object.

Identity: This characteristic is used to provide a unique name to an object. These unique names are used to differentiate the objects of a class and it also enables the objects to interact with each other. 

Sample Code:

class Student: # creating a sample class
      stud1 = "Jack"   # attributes of a class
      stud2 = "Jill" # attributes of a class
      def display( self ): # a sample method is created
             print("The student1 name is", self.stud1 )
             print("The student2 name is", self.stud2 )
s = Student( ) # object of student class got created
s.display( ) # calling the method of student class using the object

Built-In Attributes of Class:

In Python programming language, there are some inbuilt attributes present. The list of some inbuilt class attributes are given below:

  1. __dict__ : This attribute represents the dictionary and it is used to contain the namespaces of class.
  2. __doc__ : This attribute is used for string documentation if it is undefined.
  3. __name__ : This attribute represents the class name.
  4. __module__ : This attribute represents the module name in which the class is defined.
  5. __bases__ : This attribute basically contains all the base classes and also maintains the order of occurrence of base classes. 

Destroying of Objects or Garbage Collection:

Python destroys its class object by using the process of garbage collection. In Python, all the objects are automatically destroyed after the control exists from the respective class. This process happens by default in Python to free up the memory spaces. The process by which Python free up the memory by destroying the objects which are no longer going to use is known as Garbage Collection. The __del( )__ method returns the name of the class whose object is in a process of destroying.