Classes - Constructor in Python

Simple Python code using constructors. Constructors has to be declared in the __init__ function inside the class.

Source code :


class Fruit:
    def __init__(self,name):
        print("I'm " + name)
        # This is a constructor



fruit1 = Fruit("Apple")
fruit2 = Fruit("Orange")

Output :

I'm Apple
I'm Orange

Notes :

Constructors has to be declared in the init function inside the class.


Output :


                

Comments :