DataScience - OOPs Part 2

We now want to pass data in class, so we will have to design class in such manner.

we define inbuilt function '__init__()' which is constructor.

class karanskills1 :

def __init__( self , phone_number , email_id , student_id ) : self.phone_number = phone_number self.email_id = email_id self.student_id = student_id

def return_student_details (self): return self.student_id , self.phone_number , self.student_id

in 1st func, we have initialized variables and in 2nd func we have returned values.

Then we try to create object:

rohan = karanskills1()

o/p:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 rohan = karanskills1()

TypeError: karanskills1.__init__() missing 3 required positional arguments: 'phone_number', 'emai_id', and 'student_id'

Error is there bcoz we haven't passed any values, the constructor runs automatically on object creation and doesn't get required arguments fulfilled so it throws error.

So now, we create object with passing arguments:

rohan = karanskills1(9879879878987 , "rohan@gmail.com" , 101)

How it works?

We understand from the code itself that arguments go orderwise.

the values stored in the arguments are then assigned to class variables which start with "self."

eg. : self.student_id = student_id

here , argument (on right) value is assigned to class variable (on left)

If u want to access any class variable then it can be done with 'self' word. 'self' word works as a pointer.

class karanskills1 : def init( self , phone_number , email_id , student_id ) : self.phone_number = phone_number self.email_id = email_id self.student_id = student_id

def return_student_details(self) : return self.student_id , self.phone_number , self.email_id

rohan = karanskills1(9879879878987 , "rohan@gmail.com" , 101)

rohan.return_student_details()

o/p:

(101, 9879879878987, 'rohan@gmail.com')

Another object:

gaurav = karanskills1(984984984,"gaurav@gmail.com",102)

gaurav.return_student_details()

o/p:

(102, 984984984, 'gaurav@gmail.com')

For getting values of variable inside the class:

gaurav.phone_number

o/p:

984984984

The variables which give output are class variables, Eg. :

class karanskills2 : def init( self , phone_number , email_id , student_id ) : self.phone_number2 = phone_number self.email_id2 = email_id self.student_id2 = student_id

def return_student_details(self) : return self.student_id2 , self.phone_number2 , self.email_id2

gaurav = karanskills2(984984984,"gaurav@gmail.com",102)

gaurav.phone_number2

o/p:

984984984

Note : Self is NOT a reserved keyword and you can use other words also, just purpose is that we have to give any argument that behaves as a pointer.

Eg. Code:

class karanskills3 : def init( karan , phone_number , email_id , student_id ) : karan.phone_number2 = phone_number karan.email_id2 = email_id karan.student_id2 = student_id

def return_student_details(karan) : return karan.student_id2 , karan.phone_number2 , karan.email_id2

gaurav = karanskills2(984984984,"gaurav@gmail.com",102) gaurav.phone_number2

o/p:

984984984

Did you find this article valuable?

Support Karan's blog by becoming a sponsor. Any amount is appreciated!