Python Programming – Classes and Objects
Object-oriented is a term used to describe the object-oriented approach for building software. In an object-oriented approach, the data is treated as the most important element and it cannot flow freely around the system. This approach binds the data and the methods that will manipulate the data closely and prevents the data from being modified inadvertently. The object-oriented programming exhibits the following properties:
- Data hiding and abstraction
- Polymorphism (overloading/overriding)
- Dynamic binding
Here we will discuss each of these properties in brief.
Encapsulation: The wrapping of data and functions into a single unit is known as encapsulation.
Data hiding and abstraction: It refers to represent the necessary features without including the background particulars.
Inheritance: It refers to the property by which objects of one class inherit the properties of objects of another class.
Polymorphism: It refers to the ability to take more than one form. Here poly means many and morphism means forms. For example, the addition of two numbers will result in a sum however, the addition of two strings results in a concatenation of strings. That means to use single operator + for different purposes refers to operator overloading.
Dynamic binding: It refers to the linking of the procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run-time.
Alike Java, Python is also an object-oriented programming language. It possesses all the properties described above. Before indulging in concepts in great details the basic object-oriented programming (OOP) terminology is given as follows:
• Class: A user-defined prototype defines a set of attributes that describe any object of the class. The attributes are data members (class variables and instance variables) and methods, accessed via dot notation.
• Class variable: A variable that is mutually shared by all instances (objects) of a class. Class variables are defined within a class but outside any of the class’s methods.
• Method: A special kind of function that is defined in a class definition.
• Instance variable or Data member: A class variable or instance variable that holds data associated with a class and its objects or an individual object of a certain class. For example, an object obj that belongs to a class Triangle is an instance of the class Triangle.
• Instantiation: The creation of an instance of a class.
• Object: A unique instance of the user-defined data type class. An object comprises both data members (class variables and instance variables) and methods.
The class is a basic major entity of object-oriented programming language. A class can be defined by using the keyword “class”. The general syntax of defining a class in Python is as follows:
In the above syntax, we see that class is defined by using the keyword class with the ClassName, which is to be kept by the programmer and the naming conventions are the same as that applied on identifiers. Similar to functions the class statement is followed by an optional documentation string that keeps the information about the class being created. After that, the complete details of the class are given, where class variables, data members, and methods can be created and used. The programming instance of using a class is presented in Code 10.1.
Code: 10.1. Illustration of creating a class in Python.
|#illustration of creating a class in Python
def init (self, name, rollno): #initialization or constructor method
StudentstuCount += 1
def displayCount(self): #displayCount method of class Student
def display Student(self): #displayStudent method of class
In this Python programming code, we see that a class is created with the name Student, which contains a class variable student and its value will be shared by all the instances (objects) of the class. Then a method init () is created, which is a special class method known as class constructor or the initialization method. This method is called automatically when a new instance of a class is created. Two more class methods displayCount() and display student( ) are also created with an argument self. In Python, it is mandatory to write self as an argument to a class method or function. However, while calling this function, the self is not passed as an argument.
As mentioned earlier, the instance of a class is called object. Creating an object in Python is very simple. The syntax of defining an object is given as follows:
where the argument list can be the values to be supplied to the data members of the class through the constructor. The example for creating objects of the class Student given in Code 10.1. is given as in Code 10.2.
Code: 10.2. Illustration of creating objects
|stul=Student(‘John’, 111) # first object of class Student
stu2=Student(‘Clara’, 112) #second object of class Student
In Python, the attributes of a class can be accessed by using the dot(.) operator, as in C++ and Java. For example, for the above class Student of Code 10.1. the class methods display count() and display student( ) can be accessed outside the class as shown in Code 10.3.
Code: 10.3. Illustration of accessing class methods and class variable
|stu1 .displayStudent( )
In this chapter, we have discussed one of the significant concepts in programming, i.e., object-oriented programming structures in Python. We learn the creation of classes and objects. The constructor method inits () of Python is described, which is used to initialize the class members. The process to access class members is also discussed. The concept of using classes is demonstrated in detail by various programming illustrations. The built-in methods to be used within classes are also discussed. The destructor method del () to frees up memory space is also discussed.