Variables in Python
- Static variable
- Instance variable
- Local 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.
- Inside constructor using self parameter.
- class Student :
- def __init__(self,name,age):
- self.name=name
- self.age=age
- Inside Instance method using self parameter.
- class Student :
- def display(self, address, id):
- self.address=address
- self.id=id
- Outside the class using object reference.
- class Student :
- def __init__(self,name,age):
- self.name=name
- self.age=age
- def display(self, address, id):
- self.address=address
- self.id=id
- s1=Student('ritesh', 25)
- s1.height=5.6 # s.height is instance variable outside the class