6/17/2023 0 Comments Python constructor![]() In both, the object’s name and age are different. Note that we don’t count the self as a parameter here. While creating these objects, we have to pass the same number of arguments as in the _init_. ![]() Notice that we have made two objects of the Student class, namely student1 and student2. The first is self, which we will understand at some time, next is name and age. Here we have created a Student class with three parameters in the init function. And the best thing is we can also initialize the attributes of the class like name, age, etc. The property of the constructor is that whenever we initialize an object of a class, the constructor gets automatically called. It is just like the constructor in other object-oriented programming languages like c++ and java. _ init_ is a special kind of method in python. ![]() Let us now understand the exact concept and use cases of _init_ in python. For a student class, by the state, it means properties like name, age, roll number, and by the behavior, it means what type of student he/she is. It contains the state and behavior of the class. Object in PythonĪn object is the run time entity of a class and used to perform operations, declared in the class. And the operations that can be performed by the objects are calculate_marks(). Like, if we have a Student class, the variables would be named, age, section, gender, etc. In object-oriented programming, a class is a collection of related elements with the same or different data types. Object-Oriented Programming, commonly referred to as OOPs, is a type of programming language where developers define the data type of data structure and all those operations that we could perform on that data type.
0 Comments
Leave a Reply. |