Python Class And Object

Python Class and Objects

An object is an instance of a class. We can take the Shark class defined above, and use it to create an object or instance of it.

Objects can store data using ordinary variables that belong to the object. Variables that belong to an object or class are referred to as fields.

A class can be created in python by using the class keyword which is followed by the class name.

syntax of class in python

Classes are like a blueprint or a prototype that you can define to use to create objects.

 class ClassName:  


class Student:  
    Rid = 15;  
    name = "Prayag"  
    def display (self):  

In above example, the self is used to refere the variable which is refering the current class object. this is will always be the first parameter/argument in the function definition.

Creating an instance of the class

 class Student:  
     Std_id = 10;  
     Std_name = "prayag"  
     def display (self):  
         print("ID: %d \nName: %s"%(self.Std_id,self.Std_name))  
 std = Student()  


 ID: 10 
 Name: prayag