An introduction to classes and objects in python
Hello there!! As a budding coder, I have noticed that a lot of people know the definitions of objects and classes but very few understand its actual concept. Today, we will learn about the classes and their objects with respect to python.
Class and object in Python – Understanding
So, what exactly is a class? It is a collection of the data members and member functions ( i.e. methods) that are wrapped in a single block. It is basically the blueprint of an object. I would like you to read through to know more in detail below.
Creating a class in Python
A class is defined using class statement.
Let’s take an example:
class Area: "This class finds the area of circle" def __init__(self,r): self.r = r def area(self): self.result = 3.14 * self.r * self.r def print(self): print(self.result)
The line 1 is the class declaration. Here Area is the class name. You can also use any other legal identifier as a class name.
Line 2 is the documentation string. Programmers usually quote the purpose of the class here so that a user can know its function or use. However, it is optional.
>> "This class finds the area of circle"
Line 3 declares __init__ function. This is the initial function that is invoked by itself on creating an object of the class. It is called the constructor and is used to initialize the values of the object. By now you would be probably wondering what is self . It is a keyword that points to the current calling object. If you are well-versed with other programming languages, then you would understand that it is equivalent to this pointer. When a method is invoked using an instance (or object) of the class, self is passed as the first argument by default. It is, therefore, necessary to mention a formal parameter in function definition. By convention, we used self. We can even use any other legal identifier parameter name.
Line 4 is initializing and saving the data member r to the invoking object. We need to note here we access the object attribute using the dot operator with the object (self in this case).
Likewise, the line 5 declares another function Area to calculate the area of the circle. Here self is again mentioned in the parameters to store the current invoking object.
Now, Line 6 creates and stores the area in another variable result in self (that is, the calling object).
Similarly, line 7 and 8, print the area of the circle.
You may also learn,
Instantiating a class in Python
Instantiating means creating the instance of the class. Now, what is instance? Instances are also called objects of the class. They are the workable units of the program. It holds the value and occupies memory in your system. Whereas the class is simply a description of the object as to what an object will contain. It occupies no memory space. It is important to understand the difference between the two.
Syntax : object_name = class_name ( parameters if any)
Now declaring the class is not sufficient for a program to run. We need to create instances (or objects) of the class. In continuation with the first example above, let’s create the object of the class Area.
x=Area(5) x.area() x.print()
Line 1 creates the instance x of the class Area. The object invokes the __init__ method as soon as it is created. Thus, we need to pass the parameter ( 5 in this case).
Line 2 invokes the area method. As I have also mentioned earlier, the class data members and methods can be accessed by the object using dot operator. After calling this function, object x stores value of result equal to 78.5 .
Line 3 invokes the print function. Hereafter, it prints the value of result.
Purpose of objects in Python
We can create as many objects of a class as are required. These objects are exclusive of each other. Any change made in the value of one object is not reflected in the other. See the example below:
a = Area(10) b = Area(4) a.area() b.area() a.print() b.print() a.r = 5 a.area() b.area() a.print() b.print()
>> 314 #print a.r >> 50.24 #print b.r >> 78.5 #print a.r >> 50.24 #print b.r
Here, we created two objects a and b. We changed the value of the r in object a (in line 7) . But when recalculated the result of object b remained same. This shows that the changes made in one object do not affect the other.
You may also learn,
I hope this article has clarified your concepts on classes and objects, and how to create and use them in python. For further queries, comment below or contact me via [email protected] .