How to write init Class method in Python

Here is a Python sample class that uses the init method. The init method is like a constructor in Java. But the usage is different in Python.

So init method is the first method in the Python class with all the initializations. The dog_1 is an instantiation of dog class. When you print the dog_1, it prints as I am in the init method from the init method.

How to use init method

The self means this class. It is mandatory. Not only self, but you can also give any name in that place.

Instated of 'self' you can give as This_is_Python_Self. Then also it will work.

Class with init method

class dog:

   def __init__(self):

         print("I am in init method")

Now, instantiating the class



The output from the above display is as below:

I am in init method

< object at 0x7fe7b5875be0>

Related posts


Most Popular

7 AWS Interview Questions asked in Infosys, TCS

How to Decode TLV Quickly

Hyperledger Fabric: 20 Real Interview Questions