Javatpoint.com is changed to TpointTech.com
__ init __ in PythonIf you have been using object-oriented programming, you may have run into the word "init" quite often. __init__ is a Python method. It is analogous to the constructors in languages like Java and C++. Knowing classes and objects in Python will make the __init__ method understandable. Some Rpre-requisite Knowledge:
Here is a simple example of a class and an object: Output Planet Solar system I'm earth I'm a planet in Solar system AnalysisA class called Planet was made. During class:
After creating the object Earth, we used the class's two variables and method without supplying any inputs.
Now, what is self in the class?The self is substituted with the newly produced object when the function is called after creating an object for the class. It functions as the object's kind of placeholder. All the objects we generate share two variables in the class we constructed. Therefore, even if we called the variables by the names of the objects, we would still receive the same results. Let's now examine what '__init__' is capable of.
Let's look at the example from above using the __init__ method now:Output I am earth I am the 3 planet in the solar system Understanding:
When the object "earth" was created: This is the __init__ method's internal workings. The planet Earth () can have its characteristics in this fashion. If we make another item now: Output I am earth I am the 3 planet in the solar system I am venus I am the 2 planet in the solar system As a result, we can build an unlimited number of objects, each with a unique set of attribute values. The __init__ method in Python's object-oriented programming has these capabilities. Let's look at one more instance:
Output Please enter the name of the student1: Jeevani Please enter the age of the student1: 19 Please enter the name of the student2: Harini Please enter the age of the student2: 15 Stud_1.name = Jeevani Stud_2.name = Harini Understanding:We established a class called "Student" with the following three properties in the program: name, age, and email. We defined the attributes using the self-variable in the __init__ method. Two objects?stud and stud2?were produced. We already provided the email attribute value for both objects and requested the user's input for the name and age attributes before passing the values to the objects. Note:
Next Topic__dict__ in Python |
Javatpoint.com is now changed to TpointTech.com, so we request you to subscribe our newsletter for further updates.
In this tutorial, we will learn about the Python request module or how we can handle the requests using the Python requests library. We will also discuss the features of the request. Finally, we will learn how to optimize and customize those features for different situations. Introduction Generally,...
7 min read
Introduction Python is a robust and flexible programming language, and developing maintainable and successful code requires clean and effective functions. This post will examine numerous techniques for enhancing your Python functions without using cumbersome or duplicative code. By adhering to these clean coding practices, you can make...
4 min read
? The collection of Unicode characters is Python String. Python has various capabilities for string control, yet Python string library doesn't uphold the in-constructed "switch()" capability. However, there are numerous methods for reversing the string. The following reverse Python String method is being defined. Using for loop Using while...
4 min read
The power of a programming language like Python comes because of the wide range of modules and libraries it offers to its users. This time we are going to explore one of those. There might be one time or another when every one of us has...
12 min read
Python dictionaries are collections of key-value pairs that are not sorted. In this article, we'll look at how to expand a dictionary that has iously been created with new key-value pairs. Dictionary in Python : A list of entries can be turned into a dictionary in Python by...
3 min read
The logarithm function is the inverse of the exponential function. If we have an exponential equation, such as 2^3 = 8, we can rewrite it as a logarithmic equation: log2(8) = 3. Python's log base 2 functions can be accessed through the built-in math module. The...
2 min read
Data Analysis can help us to obtain useful information from data and can provide a solution to our queries. Further, based on the observed patterns we can predict the outcomes of different business policies. Understanding the basic of Data Analytics Data The kind of data on which we work...
5 min read
Introduction: In this article, we are discussing pause in python script. The input() method is used in Python 3 to collect data from the user. For programming purposes, waiting, or pausing an input task for a period may be necessary. For example, if a script contains...
3 min read
Infix: Infix expressions contain the operator in between the two operands. The operands can themselves contain operators. Though with respect to the middle operator, the expression will be an infix expression. Infix expressions are of the form (operand_1 operator oprand_2) Example: (X + Y) * (X1 + Y1) Prefix: Prefix...
3 min read
? In this tutorial, we will learn how to plot (imagine) a brain network in Python using Graphviz. Graphviz is a python module that open-source diagram representation programming. It is generally well known among scientists to do representations. It's addressing primary data as charts of conceptual diagrams...
11 min read
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