How to pass dictionary as argument in class of Python with the help of Constructor.

You could pass variable in the class of Python with the help of constructor , and that variable could be anything , It could be Dictionary , List , String , Number , etc.

First let’s make a class in Python, and pass some variable in the constructor , Whenever the new object of class will create , constructor will be called automatically.

class Doctor:
    def __init__(self,dId,dName):
        self.dId=dId
        self.dName=dName

In above code , We have made a class named as Doctor & passed dId , dName as a variable in constructor.

We could use these variable as string , integer , float & dictionary , according to our requirement .

Let’s use dId as dictionary and dName as string , To use dId as dictionary , we have to pass a dictionary in dId variable & in the case of dName , we have to pass string in dName variable like below :

class Doctor:
    def __init__(self,dId,dName):
        self.dId=dId
        self.dName=dName
dId={"jo":25,"ko":35}
dName="Doctor"

Now it’s time to use the passed dictionary in Class , to use the dictionary in Class, Let’s create another function in it , to print the value of specific key present in the Dictionary.

So, the work of function , which we are going to made in the class is to print the value of specific key present in the Dictionary, so the function will take specific key as a parameter , which is to be pass in it, After knowing the key its work will be to print the value present at that key.

Let’s make the function in class :

class Doctor:
    def __init__(self,dId,dName):
        self.dId=dId
        self.dName=dName
    def getInfo(self,keyy):
        print(self.dId[keyy])
dId={"jo":25,"ko":35}
dName="Doctor"

Above we had make a function getInfo , which will take key as a parameter , whenever it will be called by the object of class.

Whenver the object of class will be created ,dId & dName will be passed in class as argument,like below :

class Doctor:
    def __init__(self,dId,dName):
        self.dId=dId
        self.dName=dName
    def getInfo(self,keyy):
        print(self.dId[keyy])
dId={"jo":25,"ko":35}
dName="Doctor"
obj=Doctor(dId,dName)
keyy="ko"

Above , we have passed dId & dName as argument in class , because whenever any new object of class will create it automatically call constructor present in class , and in constructor we have taken dId & dName as parameter.

Now let’s call the function getInfo , by the obj object which we have created , this getInfo , will aso take keyy as argument , so we have to pass certain specific key which will print specific value , like :

class Doctor:
    def __init__(self,dId,dName):
        self.dId=dId
        self.dName=dName
    def getInfo(self,keyy):
        print(self.dId[keyy])
dId={"jo":25,"ko":35}
dName="Doctor"
obj=Doctor(dId,dName)
keyy="ko"
obj.getInfo(keyy)

Above our key = ‘ko’ , has passed which is going in getInfo , getInfo will print 35 as output, because in dId dictionary we have 35 value for the key “ko” , as you could see above in code.

You can watch below video to clear more –