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.
Some key point about Python class:
classkeyword is used to create classes in Python language.
# The class is created using class keyword class Class_Name: block of statement
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.
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
In Python programming language, there are some inbuilt attributes present. The list of some inbuilt class attributes are given below:
__dict__: This attribute represents the dictionary and it is used to contain the namespaces of class.
__doc__: This attribute is used for string documentation if it is undefined.
__name__: This attribute represents the class name.
__module__: This attribute represents the module name in which the class is defined.
__bases__: This attribute basically contains all the base classes and also maintains the order of occurrence of base classes.
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.