Classes and Objects in PythonYou 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.
Syntax: #Definition of a class Important points:
Example program: Now, let's get the overview of Objects: OBJECTS - The instances of a classNow, 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:
Examples for the characteristics of an object(Random):
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: 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 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:
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 programmingA 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. Next TopicJump Statement in Python-Break Statement |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India