Python Certification Training for Data Scienc ...
- 83k Enrolled Learners
- Live Class
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,
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,
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.
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,
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.