In this tutorial, we’ll learn what Python init function is and how to properly use it. We’ll go through multiple practical Python examples for better understanding.
Outline
- Introduction: Python init Function
- Syntax of init Function
- Implementing Python init Function (Multiple Examples)
- Example 1: Simple Python init Function
- Example 2: Python init Function with Custom Arguments
- Example 3: Find Greater Number
- Conclusion
Introduction: Python init Function
In Python class-object concept, init function is used to automatically initialize the object with some values when its created. Its used inside the class. Its automatically called when the object of that same class is created.
Let’s now practically understand how to use init function in Python class.
Syntax of init Function
def __init__(self): // body of init
- It starts with def keyword.
- Two underscores before and after the name.
- Self works as an instance of object and its necessary. Or else, exception will raised while creating object of this class. Name can be self, a, b etc.
- We can also specify other parameters to accept some values when the object is created.
- Colon(:) is necessary after the ending parenthesis.
- In the next line, we can specify body of this init method. Indentation is required.
Implementing Python init Function (Multiple Examples)
Below examples will explain the implementation and usage of Python init function.
Example 1: Simple Python init Function
class Fun: def __init__(self): print('Init runs automatically') obj=Fun()
Output
Init runs automatically
We can see that init runs automatically when the object of that specific class is created. As a result, the print function inside this init function is executed.
Example 2: Python init Function with Custom Arguments
class Fun: def __init__(self,a,b): self.name=a self.age=b obj=Fun('Zeeshan',24) print(obj.name) print(obj.age)
Output
Zeeshan 24
We’ve passed two arguments to Python init function and by using self, we’ve created two attributes and assign these values to them. For demonstration, we’ve printed the values using object of that class.
We can fetch these values within the same class as well. For that, we’ve to use self(or other name specified). See below code:
class Fun: def __init__(self,a,b): self.name=a self.age=b def values(self): // can use other name as well like selfie, a,b,value etc print(self.name) // same name should be used here as well like selfie.name print(self.age) obj=Fun('Zeeshan',24) obj.values()
Output
Zeeshan 24
In this case, name and age are not local variables. They actually belong to an object so we need to use self(or other name specified) for them like self.name or self.age.
Example 3: Find Greater Number
class Fun: def __init__(self,a,b,c): self.value1=a # can also use same name as well like self.a=a etc self.value2=b self.value3=c def findGreater(selfie): # can use self or selfie or some other name if(selfie.value1>selfie.value2 and selfie.value1>selfie.value3): print(selfie.value1+' is greatest') elif(selfie.value2>selfie.value1 and selfie.value2>selfie.value3): print(selfie.value2+' is greatest') elif(selfie.value3>selfie.value1 and selfie.value3>selfie.value2): print(selfie.value3+' is greatest') else: print('Try again') # takes three numbers from user val1=input('Enter first number:') val2=input('Enter second number:') val3=input('Enter third number') obj=Fun(val1,val2,val3) obj.findGreater() # calls the find greater method of Fun class
Output
Enter first number: 3 Enter second number: 5 Enter third number 2 5 is greatest
- We’ve taken three numbers from user and passed it as an argument to Python init function.
- After that, we’ve created a method in the same class. This method will check which one is greatest among these three numbers using Python if else conditions.
- We’ve created an object which will initialized the object and after that, we’ve called the find greater method using this object.
- As a result, the method shows which number is the greatest.
- We can use the same code(if, else, print etc.) in the init function as well but to make things look professional, we’ve used another method in the same class.
So this is how we can easily use Python init function. Feel free to share if you like this article. Also, do try it with other examples as well for better understanding.
Don’t hesitate to ask if you still have any questions regarding the implementation of Python init function. We’d be very happy to answer all.
Conclusion
To conclude this article, hope you now have a detailed understanding of how Python init function works. We’ll be very glad to receive your amazing feedback on this article.
Do visit our other articles on Python programming. Thank you for reading this article.