Init in Python

python tutorial

Init in Python

init is reserved python keyword. we are treating this keyword for creating default class constructor in python. it works with classes, __init__() is a special method of a class that initializes the variables.

__init__() method is default method which will be called whenever an object of the class is created.__init__(<reference to an object >) takes at least 1 argument by default, it is essential to pass the argument as object reference when we create __init__ function inside a class

Syntax :

__init__(<reference to an object >): <reference to an object>.<variable-name>=<value>

Example 1:   

class A: def __init__(self): self.a=10

In the above example, we have created class A which consists of __init__(self) function. Here we are initializing

self.a to 10 Object=A()

In the above code, we are calling an object for class A with name “Object” & we are using class reference A to access

__init__(self) which is defined in class A. This method is creating an object of class A named “Object” and calling

__init__(self) where ”self” in a reference to an object instance and it initializes self.a to 10

__init__() with multiple arguments

__init__() function, 1 argument is always treated as a reference to an object and rest of arguments which are passed are consider as initialization parameters

Syntax :

__init__(<reference to an object >[,param1,param2…paramn]):

<reference to an object>.<variable-name-param1>=param1

<reference to an object>.<variable-name-param2>=param2 . . .

<reference to an object>.<variable-name-paramn>=paramn

Example 2:

class A : def __init__(self, x, y): self.x=x self.y=y obj=A(10,20) obj.x obj.y

In the above example, we are passing x & y arguments along with first object reference ”self”. In __init__() function “x” and “y” are assigned to “self.x” and “self.y”

When we are creating an object of class A named “obj”, it is calling __init__() method via class constructor call “A(10,20)”.Here we are passing actual values “10” & “20” to __init__() methods arguments “x” & “y” respectively. We have defined __init__() method definition as below

def __init__(self, x, y): self.x=x self.y=y

where , self .x = x =10  and self.y=y=20 , here initialization is achieved with multiple parameters. Let’s see more examples to understand in depth.

# Create class Human which will initialize values like “Height” and “Weight”

class Human: def __init__(self): self.height=5.6 self.weight=65 obj = Human() print(obj.height) print(obj.weigh

Output :

c:/users/421209/Documents/!Python/programs>A.py 5.6 65

# Create a class Rectangle which will take length and breadth values in the class constructor call

class Rectangle: def __init__(self,length,breadth): self.length=length self.breadth=breadth obj = Rectangle(10,20) print(obj.length) print(obj.breadth)
Output:
c:/users/421209/Documents/!Python/programs>A.py 10 20

Related Blogs

Leave a Reply

Your email address will not be published. Required fields are marked *

Looking for Online Training