Python Programming – The Class Program
The complete program representing the entire concept of creating classes and objects is given in Code 10.4. Here we see that a class variable student is created and two data members i.e., name and roll no are created. The values to name and roll are assigned through the constructor method __init__( ) in Python. It contains three arguments. The first is the self, which is mandatory in Python for every function (method) to be used within the class. The other two arguments are name and roll no.
Two class methods display count() and display student() is also defined to display the total number of students and information of the students. Then, two objects soul and stu2 and created with the values of name and roll no, which are passed as arguments to the constructor function. Subsequently, the display student() method is called through stu1 and stu2 objects by using the dot(.) operator. The value of the common class variable stuCount is displayed or accessed by using the class name with the variable name student along with dot operator. The output of this code explains it all.
Code: 10.4, Illustration of using classes and objects in Python.
|#illustration of creating a class in Python
def init (self, name, rollno): #initialization or constructor method of
def displayCount(self): #displayCount method of class Student
def displayStudent(self): #displayStduent method of class
Name : John, Rollno: 111
Using a Class with Input
In the above program, we have seen that the init () method is used as a constructor to pass values to the class attributes name and roll no. What if the user wishes to give his/her input for the attributes? In order to understand this concept, consider a program given in Code 10.5. for the same class Student.
Code: 10.5. Illustration of class with input from the user.
|#illustration of creating a class in Python with input from the user
def displayStudent(self): #displayStduent method of class Student print (“Name :”, self.name, “, Rollno:”, self.rollno)
enter student name:John
It is to be noted that while using class in Python, the init () method is mandatory to be called for declaring the class data members, without which we can not declare the instance variables(data members) for the objects of the class. A variable declared outside the init () method are termed a class variable such as the student variable in the above program. It is to be noted that the class variables are accessed as className.class variable name, e.g. Student. student in the above program. However, the instance variables or data members are accessed as self. instance variable name, e.g., self. Name in the above program 10.5.
In this program, we see that the input() function is called in the definition of __init__() method for inputting the values of data variables name and rollno. Subsequently, the value of stuCount is incremented by 1 in order to keep track of the total number of objects created for the class Student. In this program, three objects stul, stu2, and stu3 are created thereby calling the constructor function init () three times. The values are assigned to name and rollno after input from the user. Then, the result is displayed by calling the displayed student() function of class Student. The output of this program can be referred to for more lucidity.
A Class Program with Computations
Here, we present another program using the concept of class and objects. We create a class inventory and compute the total price of each item along with the total bill of all the purchased items. The programming example for this problem is given in Code 10.6.
Code: 10.6. Managing inventory using classes.
|#illustration of creating and handling Inventory using class
def init (self, item, price, quantity): #constructor method of
def displayltem(self): #displayStudent method of
total no. of items: 3
total bill: 463.5
In this program, we create two class variables itemCount and total_bill, and initialized them to zero. Three class instance variables are created, item, quantity, and, price. The value of them is initialized using the int () method. In the init method, we perform computations for total price and total_bill. The display them() function displays the details of all the items. Outside the class template, we create three objects II, 12, and 13 bypassing values of three items. Then display them() function is called three times for all three objects. Subsequently, the total item count and the total bill are displayed. Users can see the output for more lucidity.
While using the concepts of classes and objects one must consider the following points:
. The_init__( ) method is mandatory while using all the classes in python. It is a constructor method used for the instantiation of the object. This method is called implicity when an object of a class is created.
• All the methods and functions used inside classes of Python language contain an obligatory argument “self’. The argument self as it suggests refers to itself- the object which has called the method. That is if you have N objects calling the method, then self. a refers to a separate instance of the variable for each of the N objects.
• The variables declared inside the int__() method are data
members or instance variables of the class, whereas variables declared outside the init () method are called class variables.
• The class variables can be thought of as static variables as in C, C++, and Java. The class variables exhibit similar properties as that of a static variable. By virtue of this, they are used to count the number of instances created for a particular class. That means the value of class variables persists between different methods/function calls.
• The class variables are accessed outside the class definition by using the className then dot(.) operator and class variable name, e.g., className.class variable name.
• The data members and member functions of a class are accessed with the object name, dot operator, and variable name or method name, e.g., ObjectName.DataMemberName or ObjectName.MethodName
• The class template begins with the class keyword, then all the details of a class begin with an indentation. In the end, the unindented mark of the class represents the complete definition or exit from the class template.
• Unlike C++ or Java, Python does not require the main() function for declaring objects of classes. Only the unidentified mark represents that the user can create objects and use them.