Variables in Python

Variables in Python

There are three types of variable in Python
  • Static variable
  • Instance variable
  • Local variable
Instance variable
  • Instance variable belongs to instance of the class.
  • For every object creation separate copy of instance variable is created.
  • Instance variable is represented by self parameter.
Creation of Instance variable
  • Inside constructor using self parameter.
  1. class Student :
  2.     def __init__(self,name,age):
  3.         self.name=name       
  4.         self.age=age 
  • Inside Instance method using self parameter.
  1. class Student :
  2.     def display(self, address, id):
  3.         self.address=address       
  4.         self.id=id
  • Outside the class using object reference.
  1. class Student :
  2.     def __init__(self,name,age):
  3.         self.name=name       
  4.         self.age=age
  5.     def display(self, address, id):
  6.         self.address=address       
  7.         self.id=id
  8. s1=Student('ritesh', 25)
  9. s1.height=5.6     #  s.height is instance variable outside the class

0 comments:

Post a Comment