Python-classes Educationinfo-4u


Object Oriented Programming :


Python Classes

       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.





  NOTE
 Everything in Python is refer as a "OBJECT"


 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 : 


class class_name :

         <statement _1>
        <statement _2>


         <statement_n>


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 .



  NOTE
"Namespace" is a place where user create/defined  an objects .



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 .


object_name = class-name()
object_name.class_member_name


 Q. Program to access class variable ?
_          
               
class student :
   roll_no=1123

obj=student()
print(obj.roll_no)

OUTPUT :
1123


    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.



  NOTE
The "self " argument refers to the object itself .

Even if we have a method which takes no arguments , then you still have to define the Method to have a self argument .
EXAMPLE :



class student :

   roll_no=1123

   def display(self):

       print("Division A")

obj= student()

print(obj.roll_no,)

obj.display()






Python Classes


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.




  NOTE

The __init__()method is same as constructor in c and c++.


EXAMPLE : to understand __init__ method.


class student():
   def __init__(self,rollno):
       self.rollno=rollno
       print("The roll Number of a student",rollno)
obj=student(1123)

OUTPUT:
The roll number of student 1123.


  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.


                           
      

  NOTE

Class variables are usually used to keep a count of number of objects created from class. 








  NOTE

Class Variables are defined at the same indentation level as that of class methods 





I hope this article is helpful for you ! 

                                      Thank u 😊