Python Programming (121 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (30 Blogs)
  • Data Science (39 Blogs)
  • Mastering Python (64 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Init In Python: Everything You Need To Know

Published on Sep 16,2019 90 Views
Vishal is a technology enthusiast working as a Research Analyst at Edureka.... Vishal is a technology enthusiast working as a Research Analyst at Edureka. He has expertise in domains like Big data, Machine Learning,Statistical Analysis and...
16 / 59 Blog from Python Fundamentals

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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. This article will help you explore this concept and following pointers in detail,

So let us get started then,

Init  in Python

Introduction To Init Function

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.

Class

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.

Object

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.

Self

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

__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.

#1 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.

class Car(object):
"""
blueprint for car
"""
def __init__(self, model, color, company, speed_limit):
self.color = color
self.company = company
self.speed_limit = speed_limit
self.model = model
def start(self):
print("started")
def stop(self):
print("stopped")
def accelarate(self):
print("accelarating...")
"accelarator functionality here"
def change_gear(self, gear_type):
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("ertiga", "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,

#2 Example

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:
def __init__(self, length, breadth, unit_cost=0):
self.length = length
self.breadth = breadth
self.unit_cost = unit_cost
def get_perimeter(self):
return 2 * (self.length + self.breadth)
def get_area(self):
return self.length * self.breadth
def calculate_cost(self):
area = self.get_area()
return area * self.unit_cost
# breadth = 120 cm, length = 160 cm, 1 cm^2 = Rs 2000
r = Rectangle(160, 120, 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 bind 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.

To get in-depth knowledge on Python along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access. Got a question for us? Mention them in the comments section of “Python Tutorial” and we will get back to you.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.