Categories
100DaysOfCode DATA MINING DATA SCIENCE DJANGO EMBEDDED PROGRAMMING IOT MACHINE LEARNING PROGRAMMING PYTHON TUTORIALS

Python Init Function Example, Syntax & Best Practices

In this article, we will take a look at the Python Init Function using an example. At first, we learn about it’s syntax and how we can use it. But once we are comfortable with it, we will then learn the best practices to use when we want to call it!

So, does this plan sound good? Yes? Then let’s do it! 😉

Python Init Function Example and its syntax
Python Init Function Example and its syntax explained in 5 minutes

What Is The Syntax Of Python Init Function?

So first, let us start with how the syntax of a Python Init function looks like. Now, for us to know about it, we will have to take a look at the Python’s official document. From this, we now get to know that the init function is a method of a class and it looks like:

def __init__(self)

From the above code, we can see that the init function takes at least one argument. This is called as ‘self‘ and it has got a special meaning to it. But before we learn about it, let us first know how Python’s Class works.

You see, in Python a Class can have many methods in it. But it will have one method in it for sure. It is called the init() method. Now this method will be present in a Python Class even if you have not declared it yourself!

So, in other words, init() is a function that you will always find in any Class of Python. If you declared it yourself, it is an explicit init() function. But if you did not, it will be an implicit init() function!

Alright, so we now that an init() function is always present in any Python Class, what does it do? We will answer this question next!

Why do we need an init() function in Python?

So to answer this, we need you to have some knowledge about Object Oriented Programming (OOP). So if you are not so familiar with it, please go and learn about constructors in OOP. But for now, I will assume that you do know about OOP.

So as you know in OOP, we have a constructor method in every Class. So whenever we create an object using a Class, its constructor method will get called. Right?

In the same way, in Python the init function acts as a constructor. So it will get called whenever we create a Python’s Class object.

So this is why I said that the init() function is always present in a Python Class. So, the syntax of the init() that you saw above is how it looks like when it is implicit.

What does the self parameter do?

In order to answer this, let us take a look at the syntax once again

class Test:
    def __init__(self)

Here, we are looking at the code of an example Python class called Test. So here we can see that the init function has one argument called self.

Now, in order to for us to understand what this self argument does, let us first create an object of the Test class type.

t = Test()

So in the above code, we created a new object instance of the Test class. Now, as soon as this code runs, the init() function of the Test class will get called. But what is getting passed as the self argument here the variable t itself.

So, we now see that the self argument of the init() function points to the instance of the object itself!

Are you clear until now? Good! Then let us now look into an example of Python Init() function.

Python Init Function Example

So, let us take a look at this Python Init function example code

class Point:
    def __init__(self, x, y):
        _x = x
        _y = y

p = Point(1,2)

From the above code, we can see that we now have a new Class called Point. We also have an init() function that we have defined explicitly. This init() function takes in three arguments self, x & y.

Is it all clear? Good!

So we can quickly see the advantage of using init() function here. With the help of this function, we are able to set the initial values of x & y variables!

So when we created an instance of Point class using the code:

p = Point(1,2)

we were able to set the p object’s x & y values to 1 & 2 right away!

So, if things are clear until this point, we will then move on to the last part of this article.

Best Practices With Python Init Function

So here is a list of best practices you should follow when writing an init function:

  • Always use Init() function to initialize values to object parameters
  • Reduce the number of parameters passed to the init() if possible. Do not over use it.
  • Keep the init() function simple. Do not abuse it!

Conclusion

So that is about it! That is all you need to know about Python init() function. We have discussed everything there is to know about Python’s init() function with an example. We also went through the best practice we should follow when making use of it.

So with this, I will end this article here. If you still have any more questions, please ask it in the comment section below!

Know the difference between an expression Vs statement in Python

So until next time, have a nice day! 🙂

Categories
DJANGO PYTHON TUTORIALS WEB DEVELOPMENT WEB SERVER

Django Web Framework Beginner Tutorial – Introduction

What is Python Django?

Django is a Python based web development framework. It is a collection of libraries and tools that can be used to develop websites and web applications. Django uses Python as its primary backend programming language.

Learn more about Frontend & Backend components of a web app here

Why use Django Web Development Framework for developing a web app?

In the early days of internet, not many programming languages or supporting libraries were available for the development of websites. So, every website developer was writing many frequently used components repeatedly.

Python Django Web Development Framework

These included features like user authentication, database read/write, Cross Site Scripting (XSS)/malware protection code, database injection prevention code etc.

Every time a new website was built, web developer had to rewrite these pieces of code over and over again. This results in an increase in time to complete the project. It also exposes the website to certain vulnerabilities due to bad testing or bad design.

In order to overcome these, developers started to create a common web development framework. This contained all the frequently used components like authentication, session management code etc. These were later made available to others as part of the web framework libraries.

Soon enough, these libraries started being developed in different programming languages as well. Django is one such web application development framework that was developed using the Python programming language.

Why Use A Web Development Framework Like Django?

The main advantage of using Django is the number of readily available components it comes with. All the bells and whistles required to develop a basic web application is present in Django. Module like user management, admin dashboard, session management component, protection against XSS, CORS support are all readily available. This makes Django one of the quickest web development framework to get started with. You can go live with a website in no time because of this.

In addition to this, Django also comes with framework extensions such as Django Rest Framework (DRF) that can be also used to enhance the capabilities of a Django Web Application.

All these features of Django makes it one of the most appealing “all batteries inclusive” web app development framework in the tech industry.

In addition to this, if you are already familiar with the Python programming, then using Django becomes very easy.

Django is not the only Python web development framework. There exists other Python based web development frameworks like Flask, web2py and many more. But what makes Django different and easier to get started with is its all inclusive battery modules we discussed earlier.

Who is currently using Python Django Web Framework in real world?

Some of the top tech companies using Python Django includes Instragram, Quora, Mozilla, Disqus, National Geographic, Last.fm etc.

This was a theoretical introduction of Django. In the upcoming articles, we will get our hands dirty by using Django to develop a few simple web apps. This should give you a clear idea on the advantages of Django and why it is extremely useful.