Use __init__ Method In Python Concept
Table of Contents
Use __init__ method in python it means self method use in class. In previous article we have covered the oops in python. It is really difficult to understand any Object Oriented Programming but in this tutorial we see it deeply and I will explain it very basic and simple examples.
Mostly programming use this __init__ method in python to make the programm fast and save the developer time. By using these oops we execute the hindered of method in few mile second.
Before starting the __init__ method in python first you need to understand the some prerequisites.
the __init__ method is like a contstructer which you hve already use in c++ ,java and php programming. The constructer are use the initialize the state of object in class. It assign the value of data member when the class object is created. It is the collection of data it execute at the time when object is created. Following is the simple example of __init__ method in python.
Example :
# A Sample class with init method
class student:
# init method or constructor
def __init__(self):
print('__init__method')sadaqat= student()
raza = student()
ali = student()OutPut is:
__init__method
__init__method
__init__method
In the above example we can’t assign any argument in object so it give me the three time print because I enter the three student in student class.
In next example we assign the two argument to the student class after self.
Example :
# A Sample class with init method
class student:
# init method or constructor And Argument
def __init__(self,course, DOB):
print(course,DOB)
sadaqat= student('SCIENCE','2020-11-01')
raza = student('ARTS','2020-10-06')
ali = student('COMPUTER','2020-09-04')
The OutPut is:
SCIENCE 2020-11-01
ARTS 2020-10-06
COMPUTER 2020-09-04
Access Any Object Data In Print Function:
Before accessing any class object in print function first you need to declare argument in self method otherwise you can’t access it.
Example :
# A Sample class with init method
class student:
# init method or constructor, augument
def __init__(self,course, DOB):
print(course,DOB)
self.course= course
self.dob=DOB
sadaqat= student('SCIENCE','2020-11-01')
raza = student('ARTS','2020-10-06')
ali = student('COMPUTER','2020-09-04')
print('==================================')print('SADAQAT COURSE IS :',sadaqat.course)
print('SADAQAT DOB IS :',sadaqat.dob)
OutPut Is:
SCIENCE 2020-11-01
ARTS 2020-10-06
COMPUTER 2020-09-04
==================================
SADAQAT COURSE IS : SCIENCE
SADAQAT DOB IS : 2020-11-01
Pingback: Free PHP MySQL POS With Source Code 2021 | GSS TECHNOLOGY