Must-Know Init in Python & How to Master It
Last updated on 13th Jun 2020, Blog, General
Python is one of the most popular coding platforms available in the industry today. Starting from amateurs to professionals, everyone used Python to code and make applications for mobile as well as web. Being such a versatile platform, there are some aspects that are not so well known among users. One of the most significant of this is the Init In Python.
Init in Python
If you have been using Python for sometime now, you are well aware of the fact that Python is an object oriented programming language. What this basically means is that everything that you create in the Python environment is termed as being object. Now before we begin to explore more about the __init__ function in Python, let’s get the basics out of the way.
A class in Python is a category or set of different elements grouped together that share one or more similarities with one another, but yet distinct from other classes via type, quality and kind. In technical terminology, we can define a class in Python as being a blueprint for individual objects with same or exact behavior.
An object in Python is one instance of a class and it can be programmed to perform the functions that have been defined in the class.
The self in keyword in Python is used to all the instances in a class. By using the self keyword, one can easily access all the instances defined within a class, including its methods and attributes.
__init__ is one of the reserved methods in Python. In object oriented programming, it is known as a constructor. The __init__ method can be called when an object is created from the class, and access is required to initialize the attributes of the class.
Moving on with this article on Init In Python,
Use of init in Python
From the definition of __init__ shared above, you now have somewhat of an idea at what this method exactly does. In order to further clarify this concept, let’s look at an example.
Aim: To program a racing game in Python with the name “NFS.”
Solution: If you want to create a racing game in Python with the name “NFS” one of the basic objects that you need to create are the individual cars. Each of the cars that you create within the game will all have different attributes, for example color, speed etc. as well as methods like change gear, accelerate, break etc.
When you code this concept into the Python interpreter it should look something like this.
Get Comprehensive Python Training to Build Your Career
- Instructor-led Sessions
- Real-life Case Studies
- Class car(object):
- Blueprint for car
- Print(“gear changed”)
- “gear related functionality here”
- Now that we have created the objects, let’s move on to create the individual cars in the game.
- Maruthi__suzuki= Car(“eriga”,”black”,”Suzuki”,60)
- Audi = Car(“A6”,”red”,”audi”,80)
In the above example, we have created two different car models; one being the Suzuki Ertiga and the second Audi A6. Once these objects have been successfully created, we can make use of the __init__ method to initialize and thus prepare for the next steps.
In this example, we can also make use of the self method to represent the different instances of the class and also bind the attributes with the given arguments. Using the self method will allow us to basically access the attributes and methods that we have created within the class.
Moving on with this article on Init In Python,
Aim: To find out the development cost of a rectangular field having the dimensions, breadth(b=120), length(l=160). The cost of 1 square metres is 2000 INR.
Solution: Keeping in mind the steps shared in the earlier example, the code for this particular example will look like the following.
- Class Rectangle:
- Return 2*(self.lenth + self.breadth)
- Return self.length*self.breadth
- Def calculate_cost(self):
- Return area*self.unit__cost
- #breadth =120cm,length=160cm, 1cm^2=Rs.2000
- Print (“Area of Rectangle: %s cm^2 % (r.get_area()))
- Print(“cost of rectangular field: Rs. %s “ %(r.calculate__Cost()))
As discussed in the earlier example, the self method represents the instances and attributes of the class. If you take a closer look, you will realise that we have used the methods, self.length to derive the value of the attribute length. The attribute length is already bound within the class, and we are using the self method to represent the object within the same class.
We have also made use of the method, def get_area(self): as a parameter in the above code. What this does is, every time we call upon the method it automatically passes the first argument along with other arguments in the method. Although this automation might look small at first glance, it will save a lot of time and increase efficiency in the long run.
To further clarify this discussion, take a look at the example below.
- r = Rectangle(160, 120, 2000)
Note: “r” is the representation of the object outside of the class and “self” is the representation of the object inside the class.
This brings us to the end of this article on Init In Python.