Object Oriented Programming :
The unique selling point of Python Programming language is it supports all types of Programming, i.e. Object oriented Programming ,Procedure Programming and Functional Programming. Till now we have defined Variables, functions and Modules that is what exactly Procedure Programming means . Completing tasks without changing data with help of functions called as Functional Programming .
Object oriented Programming define as the programming methodology which is totally based on " OBJECTS ,these objects defined under classes .OOP consist of Classes ,Methods(functions),and Objects.
Classes and Objects :
Classes and objects are the two main aspects of object orientated programming . Class is basic Building block of python. and object in class is like a variable.Class provides a blueprint or a template using which objects are defined .
Defining Classes in Python :
Python have a special syntax to define a class ,we have to use "class" keyword to define a class.
SYNTAX :
Defining a class is similar as defining a Function . It start with keyword class followed by the Class name and colon [ : ].Class can every type of statement like control statement ,Loops also have function definition. Variable define inside class know as class variable ,functions inside class is called " Methods ". Class variables and methods are called as class Members .and Class member are access by Class Object .
Python have a special syntax to define a class ,we have to use "class" keyword to define a class.
SYNTAX :
Creating an Object :
Once we define a class next task is to create an object ,to access the class members .There are two ways to define/create an object. To access class variable and class methods
we use (.) dot operator .
Q. Program to access class variable ?
_
Once we define a class next task is to create an object ,to access the class members .There are two ways to define/create an object. To access class variable and class methods
we use (.) dot operator .
Q. Program to access class variable ?
_
In the above program , we have define a class name as student with variable roll_no with value as 1123.The object of class student is created to access class variable .
Class Method:
Class methods are similar to that functions , the functions define inside a class are know as Methods .There is One difference in Method and function , i.e.when we define a method the first argument in it is "self" ,This is the first argument that is added in the parameters list. If a user don't pass value for this argument python provides value automatically.
Even if we have a method which takes no arguments , then you still have to define the Method to have a self argument .
EXAMPLE :
Key Points to remember :
1. The statements inside class must be properly Indented .
2. An empty class is not allowed in python ,so if a class having no statements the pass statement must be there .
3. Class methods that begins with double underscore (__) are a special function with predefined and special meaning .
The __init__()method :
The __init__ method have a special significance in python classes. __init__ method is nothing but class constructor .The function of __init__() method is to assigned the values to class members when an object of a class is created .The method is useful to initialize the variables of the class object. Important thing is __init()__ method is prefixed as well as suffixed by
double underscores.
EXAMPLE : to understand __init__ method.
In the above program ,we had define a class name as student .in that we have __init__() method who accept two arguments i.e. self and rollno .In __init__() method we define a variable as a self.rollno which has exactly the same as that specified in the argument list .But both are different variables. The self.rollno belongs to the newly created object .Note we have just create a new object but not called __init__() method .this is because __init__() method get's call automatically when object of a class is created.
Types of Variables in Class :
Classes have two types of variables i.e. class variables and
instance variable .
Class Variable : The variable which is define inside a class but outside __init__method is called as Class Variable.
Instance Variable : When a variable define inside __init__()
method the it is called as Instance Variable.
Class Method:
Class methods are similar to that functions , the functions define inside a class are know as Methods .There is One difference in Method and function , i.e.when we define a method the first argument in it is "self" ,This is the first argument that is added in the parameters list. If a user don't pass value for this argument python provides value automatically.
Even if we have a method which takes no arguments , then you still have to define the Method to have a self argument .
EXAMPLE :
Key Points to remember :
1. The statements inside class must be properly Indented .
2. An empty class is not allowed in python ,so if a class having no statements the pass statement must be there .
3. Class methods that begins with double underscore (__) are a special function with predefined and special meaning .
The __init__()method :
The __init__ method have a special significance in python classes. __init__ method is nothing but class constructor .The function of __init__() method is to assigned the values to class members when an object of a class is created .The method is useful to initialize the variables of the class object. Important thing is __init()__ method is prefixed as well as suffixed by
double underscores.
Types of Variables in Class :
Classes have two types of variables i.e. class variables and
instance variable .
Class Variable : The variable which is define inside a class but outside __init__method is called as Class Variable.
Instance Variable : When a variable define inside __init__()
method the it is called as Instance Variable.
I hope this article is helpful for you !
Thank u 😊
Thank u 😊