Javatpoint Logo
Javatpoint Logo

Classes and Objects in Python

You wanted to build a house. What is the first thing you do to start the building process? You create a plan on how you want your house. You follow the plan to build the house. A plan is like a blueprint of the house that is not built yet and will be built based on it.

Why do we even need a plan? For the organization of all components like different rooms, walls, windows, and doors in the right places with the correct dimensions.

Definition of a class:

In any programming language, a class is a user-defined plan or blueprint using which objects or instances of the class are created.

You may wonder why we need classes in programming. We can create something like a variable or a structure, store what we want, and use it.

  • A class can have its attributes - variables and functions with pre-stored values and functionalities.
  • Example situation: If we want to store the data of different age groups of children and their details in an orphanage :
  • We cannot just create a list and keep on storing the ages and names of the children without any organization.
  • Creating multiple lists for multiple age groups - one list to store ages, one for names, another to match - becomes complex and leads to ambiguity.

Syntax:

#Definition of a class

Important points:

  1. "class" is a keyword in python libraries.
  2. A class name must be capitalized.
  3. Attributes are the variables owned by the class (declared inside the class) that the created objects can use.
  4. Methods are the functions owned by the class (defined inside the class) that the created objects can use.
  5. The created objects using the dot (.) operator uses these attributes and methods of a class.

Example program:

Now, let's get the overview of Objects:

OBJECTS - The instances of a class

Now, we have the blueprint. This is the time for action and implementing the idea of the class. The house - the plan is ready. It is time to start the construction. The organizing pattern is ready. Now, we build the rooms one by one. You can assume one of the objects of the house class is a room. We can create many objects-many different rooms. An object is like a specimen of the class.

The characteristics of an object:

  1. State: The state of an object refers to the attributes of the object - different variables with different info about the object.
  2. Identity: It is the object's name to identify every object uniquely from another.
  3. Behavior: The behavior of an object refers to the methods of object-different functionalities.

Examples for the characteristics of an object(Random):

Classes and Objects in Python
Object Identity State Behavior
A person Rakesh Literate, qualified, vegetarian Speaking, walking, reading
A shopping sale Republic day sale Started, offers, Ended, Cancelled Earn loyalty points

If we created a class, say, people, a person is an object in that class. The person's name is the unique identity to identify the object; variables like literate and vegetarian refer to the object's state - different attributes of the object. The functions like walking; sleeping explains the person's behavior (object).

If the object is a dog:

Classes and Objects in Python

Declaring an object:

Object_name = Class_name()

Depending on the need, we can create as many objects as we want for a single function. So, we create a class and store the blueprint to do a task or organize something; we store them in variables called attributes and functions called methods.

Then, in the program, when we need the functionality of a class or its attributes and methods, we access them by declaring an object of that class.

Syntax to access:

Let us take the example above - A Dog.

Program:

Output:

The color of my dog is: Brown

Now, going to the methods of a class, we need to learn about the self variable and the __init__ method.

  • The __init__() method is a constructor which will be invoked by default when a new object is created and initialized.
  • "self" is not a keyword in python. It is a special variable. When we declare a method in a class, we need to declare the first argument as self.
  • We will be able to access the attributes and the methods of the class using this self-argument. It holds the arguments to the attributes.
  • Even in the init method, we must declare the first argument as self.
  • Self determines the current instance of the class; In the case of the init method, it refers to the newly created object, while in other methods, it refers to the object whose method is called.
  • Even if we want to create a method with no arguments, we need to keep the self variable as the only argument in the method.

The syntax will look like this:

Example programs:

Output:

Please enter the name of the student1: Santhosh
Please enter the age of the student1: 19
Please enter the name of the student2: Rakesh
Please enter the age of the student2: 19
Stud_1.name = Santhosh
Stud_2.name = Rakesh

Output:

Enter the name of the person1: Mahesh
Enter the name of the person2: Rakesh
Person P1 name: Mahesh
Person P2 name: Rakesh

Attributes for an Empty class:

Python language allows the programmer to create attributes of an object declared for an empty class. We can even modify the values of attributes in different objects.

Here is an example of an empty class "Student_info". In the body of the program, we created an object Stud_1 for the class and gave values to 3 attributes for the class:

Output:

Stud_1.name: Sonu
Stud_1.age: 19
Stud_1.graduate: B-tech

We discussed above that the init method is a constructor. Diving deep into Constructors in python:

A constructor can be understood as a type of a method. We use this to perform actions or tasks like initializing variables and other tasks that must be done when a new object is created.

In python, we use the same __init__(self) in all classes. The conventions of the name: Two leading and trailing underscores. It is designed this way to prevent ambiguity with user-defined methods.

If we create a class without using the constructor, by default, python creates a constructor which doesn't have functionality other than instantiating the created object.

Let's summarize the learned topic till now:

Every class in python will consist of 3 key partners:

  1. The constructor
  2. Attributes of the class
  3. Methods of the class

We create a class. Inside the class, we build the constructor __init__() with self argument and other arguments that we bind with the class attributes inside the body. So, when we create an object with some parameters, these parameters will occupy the arguments and be stored in the class attributes.

It is the same in normal methods, but in normal methods, we will have a task going on, but in the init, it is only about initializing the arguments.

The self keyword helps bind the parameters and arguments of the constructor and other methods. It must be declared as the first argument in any method.

But, what is all this concept of classes and objects about?:

OOPS - Object-oriented programming

A programming paradigm explains the methodology of designing and implementing programs in a particular programming language. It demonstrates how a program is analyzed and solved.

OOPS is a frequently used programming paradigm, and python follows this paradigm. As the name itself suggests, it is based on the concept of objects and classes.

There are many concepts in OOPS that are clever and useful for solving complex problems without ambiguity. This style of programming revolves around objects.

Using objects, the whole program and every aspect of it can be divided into objects and dealt with more easily. Simply that makes the program understandable productive with less maintenance cost.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA