Types of Variables:
§
Inside Python class 3 types of variables are allowed.
1.
Instance Variables (Object Level Variables)
2.
Static Variables (Class Level Variables)
3.
Local variables (Method Level Variables)
1. Instance Variables:
§
If the value of a variable is varied from object to object, then such
type of variables is called instance variables.
§
For every object a separate copy of instance variables will be created.
§
Instance variable is an object level variable.
Where and How we can declare Instance
variables:
1. We can declare instance
variable Inside Constructor by using self-variable
2. We can declare instance
variable Inside Instance Method by using self-variable.
3. We can declare instance variable
Outside of the class by using object reference
Variable
§ We can declare instance variables inside a constructor by using self-keyword.
Once we create object, automatically these variables will be added to the object.
class Student:
def
__init__(self):
self.name="Scott"
self.rollno=101
self.age=20
#Create
an Object
s1=Student()
print(s1.__dict__)
{'name': 'Scott', 'rollno': 101, 'age': 20}
2. Inside Instance Method by using self-variable:
§ We can also declare instance variables inside instance method by using self-
variable. If any instance variable declared inside instance method, that instance
variable will be added once we call that method.
Example:
class Demo:
def
__init__(self):
self.a=10
self.b=20
def
f1(self):
self.c=30
#
instantiate the Demo class d1=Demo()
print(d1.__dict__)
d1.f1()
print(d1.__dict__)
OutPut:
{'a':
10, 'b': 20}
{'a': 10, 'b': 20, 'c': 30}
3. Outside of the class by using
object reference variable:
§ We can also add instance variables outside of a class
to a particular object.
Example:
class Demo:
def __init__(self):
self.a=10
self.b=20
def f1(self):
self.c=30
#
instantiate the Demo class
d1=Demo()
print(d1.__dict__)
d1.f1()
print(d1.__dict__)
d1.d=40
OutPut:
{'a':
10, 'b': 20}
{'a':
10, 'b': 20, 'c': 30}
v How We can access Instance variables With-in Class and Outside the
class:
§ We can access instance variables with in the class by
using self-variable and
outside of the class by
using object reference.
class Demo:
def
__init__(self):
self.a=10
self.b=20
# With-in Class We can access instance variable by
Using self variable
def
info(self):
print("a= ",self.a)
print("b= ",self.b)
#
instantiate the Demo class
d1=Demo()
d1.info()
print("=============================")
#Outside
the class instance variable, we can access by Using reference variable
print("a= ",d1.a)
print("b= ",d1.b)
Output:
-
a= 10
b= 20
=============================
a= 10
b= 20
v How We can delete Instance variables With-in Class and Outside the
class:
§
We can delete instance variable by using del kwd.
§
We can delete instance variable With-in class as follows
§
We can delete instance variable outside of class as follows
del objectreference.variableName
class Demo:
def __init__(self):
self.a=10
self.b=20
self.c=30
self.d=40
self.e=50
def removeA(self):
del self.a
# instantiate the Demo class
d1=Demo()
print(d1.__dict__)
d1.removeA()
print(d1.__dict__)Output:
{'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e':
50}
{'b': 20, 'c': 30, 'd': 40, 'e': 50}
Output:
Note: -
Output:
Note:-
Output
variable outside the class.
Output
class Demo:
def __init__(self):
self.a=10
self.b=20
self.c=30
self.d=40
self.e=50
# instantiate the Demo class
d1=Demo()
print(d1.__dict__)
del d1.a
print(d1.__dict__)Output:
{'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e':
50}
{'b': 20, 'c': 30, 'd': 40, 'e': 50}
Note: -
§ The instance variables which are deleted from one object,
will not be deleted from
other objects because for each object a separate copy
will be maintained.
class Demo:
def
__init__(self):
self.a=10
self.b=20
self.c=30
self.d=40
self.e=50
#
instantiate the Demo class
d1=Demo()
d2=Demo()
print(d1.__dict__)
print(d2.__dict__)
print("========================================")
del d1.a
print(d1.__dict__)
print(d2.__dict__)
Output:
{'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e':
50}
{'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e':
50}
===============================================
{'b': 20, 'c': 30, 'd': 40, 'e': 50}
{'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e': 50}
§ For instance, variable a separate copy will be maintained for every
object.
if we are change the value of instance variables of one object then
those
change will not be reflected to the remaining object of that class.
class Demo:
def __init__(self):
self.a=10
self.b=20
# instantiate the Demo class
d1=Demo()
print("Before changing d1 => ",d1.a,d1.b)
d1.a=30
d1.b=40
d2=Demo()
print("For d2=> ",d2.a,d2.b)
print("===================================")
print("After changing d1 => ",d1.a,d1.b)
print("For d2=> ",d2.a,d2.b)
Output
Before changing d1 => 10 20
For d2=> 10 20
===================================
After changing d1 => 30 40
For d2=> 10 20
Static variable In Python
§ Static
variable is used for fulfil the common requirement. For Example, company
name
of employees, college name of students etc. Name of the college is common for
all students.
When and why we use static variable
§ Suppose we want to store
record of all employee of any company, in this case
employee id is unique for
every employee but company name is common for all.
§ When we create a static
variable as a company name then only once memory is
allocated otherwise it
allocate a memory space each time for every employee.
Static variable
§ If the value of a variable is not varied
from object to object then it is never
recommended to declare that variable at
object Level, we have to declare such type
variable of variables at class
level.
§ In the case of instance variable for every
object a separate copy will be created.
but In the case of static variable only
one copy will be created at class level and
share that copy for every object of
the class.
§ static variable will be created at the
time of class Loading & destroy at the time of
class unloading. Hence the
scope of the static variable is exactly same as the scope
of the class
Note-
§
If
the value of a variable is varied from object to object then we should go for
instance variable. In the case of instance variable for every object a separate
copy
will be created.
§
If
the value of a variable is same from all object then we should go for static
variable. In the case of static variable only one copy will be created at class
level and
share that copy for every object of the class.
Where and how we can declare variable as
static variable with-in class
.
§ Static variables we can declare with in the class
directly but outside of any method or
block or constructor.
class Student:
college_name='ITM'
def __init__(self,rollno,name):
self.rollno=rollno
self.name=name
§ This class has two instances and one static variable.
§ Static
variables we can declare also inside constructor by using class name
class Student:
def __init__(self,rollno,name):
Student.college_name='ITM'
self.rollno=rollno
self.name=name
§ Static
variables we can declare also inside instance method by using class name
class Student:
def __init__(self,rollno,name):
self.rollno=rollno
self.name=name
def m1(self):
Student.college_name='ITM'
§ Static
variables we can declare also Inside classmethod by using either class
name or
cls variable.
class Student:
def __init__(self,rollno,name):
self.rollno=rollno
self.name=name
@classmethod
def m1(cls):
cls.college_name='ITM'
Student.college_name='ITM'
§ Static
variables we can declare also Inside static method by using class name
class Student:
def __init__(self,roll_no,name):
self.roll_no=roll_no
self.name=name
@staticmethod
def m1(self):
Student.college_Name="ITM"
How we can access static variable
§ We can access static variable inside constructor by
using either self or
classname
class Student:
college_Name="ITM"
def __init__(self):
#acessing
static variable using self inside constructor
print(self.college_Name)
#acessing
static variable using ClassName inside constructor
print(Student.college_Name)
# instantiate the Student class
s1=Student()
v We
can access static variable inside
instance method: by using either self or
classname
class Student:
college_Name="ITM"
def m1(self):
#acessing
static variable using self inside Instance Method
print(self.college_Name)
#acessing
static variable using ClassName inside Instance Method
print(Student.college_Name)
# instantiate the Student class
s1=Student()
s1.m1()
v We
can access static variable inside class method:
by using either cls variable
or classname
class Student:
college_Name="ITM"
@classmethod
def
m1(cls):
#acessing static variable using cls
inside class Method
print(cls.college_Name)
#acessing static variable using ClassName
inside class Method
print(Student.college_Name)
#
instantiate the Student class
s1=Student()
s1.m1()
v We can access static variable inside static method: by
using classname
class Student:
college_Name="ITM"
@staticmethod
def m1():
#acessing
static variable using ClassName inside static Method
print(Student.college_Name)
# instantiate the Student class
s1=Student()
s1.m1()
v We can access static variable From outside of class:
by using either object
reference or classname
class Student:
college_Name="ITM"
# instantiate the Student class
s1=Student()
#acessing
static variable using ClassName Outside the class
print(Student.college_Name)
#acessing static variable using object reference Outside the
class
print(s1.college_Name)
How and Where We
can modify the static variable
§
If you have created both the instance and static variables with the same
name, then if
the variable is accessed from the reference variable outside the
class, then the instance
variable will be accessible, in this case static will
not be accessible using reference
variable. then we can access static variable
outside the class by using class name.
v
We can modify static variable
inside constructor using className
class Student:
college_Name="ITM"
def __init__(self):
Student.college_Name="IIT"
#
instantiate the Student class
s1=Student()
#acessing
static variable using ClassName Outside the class
print(Student.college_Name)#The Output:IIT
#acessing
static variable using object reference Outside the class
print(s1.college_Name)#The Output:IIT
v We can modify static variable inside staticMethod
using className
class Student:
college_Name="ITM"
@staticmethod
def m1():
Student.college_Name="IIT"
#
instantiate the Student class
s1=Student()
#acessing
static variable using ClassName Outside the class
print(Student.college_Name)#the output:ITM
#acessing
static variable using object reference Outside the class
print(s1.college_Name)#the output:ITM
s1.m1()
print("After
modifying static variable through static Method")
#acessing
static variable using ClassName Outside the class
print(Student.college_Name)#the output:IIT
#acessing
static variable using object reference Outside the class
print(s1.college_Name)#the output:IIT
v
We can modify static variable
inside classMethod using cls variable
class Student:
college_Name="ITM"
@classmethod
def m1(cls):
cls.college_Name="IIT"
# instantiate the Student class
s1=Student()
#acessing static variable using
ClassName Outside the class
print(Student.college_Name) #the output:ITM
#acessing static variable using
object reference Outside the class
print(s1.college_Name) #the output:ITM
s1.m1()
print("After modifying static variable through class Method")
#acessing static variable using
ClassName Outside the class
print(Student.college_Name) #the output:IIT
#acessing static variable using
object reference Outside the class
print(s1.college_Name) #the output:IIT
v
We can modify static variable
outside the class using className
§
In this example, we have defined a static variable named college_name
we will be trying to be changed the static variable value with the help
of reference variable outside the class.
class Student:
college_Name="ITM"
# instantiate the Student class
s1=Student()
#acessing static variable using
ClassName Outside the class
print(Student.college_Name)
#acessing static variable using
object reference Outside the class
print(s1.college_Name)
#modifying the static variable outside
class
Student.college_Name="IIT"
print("After modifying static variable outside class")
#acessing static variable using
ClassName Outside the class
print(Student.college_Name)
#acessing static variable using
object reference Outside the class
print(s1.college_Name)
v
What happened when we are
trying to modify static variable value by using self
or object reference
variable.
§
When we are trying to modify static variable value by using object
reference or self then the value of static variable won't be changes, just a
new instance variable with that name will be added to that particular object.
class Student:
college_Name="ITM"
def m1(self):
self.college_Name="IIT"
# instantiate the Student class
s1=Student()
s1.m1()
#acessing static variable using
ClassName Outside the class
print(Student.college_Name)
#acessing instance variable using
object reference Outside the class
print(s1.college_Name)
Output
ITM
IIT
Program
§ Let
us say we want to assign a unique number to each product object. The first
product object should be given a number 100 and subsequent product objects
should have that value increased by 1. We can accomplish this by using a
combination of static and instance variables as shown below:
class Product:
productId=100
def __init__(self,name,price,brand):
self.name=name
self.price=price
self.brand=brand
Product.productId +=1
def displayPrdouctDetails(self):
print("ProductId:",Product.productId)
print("ProductName:",self.name)
print("ProductPrice:",self.price)
print("ProductBrand:",self.brand)
prod1=Product('Ipad',10000,'Apple')
prod1.displayPrdouctDetails()
print("============================")
prod2=Product('Redmi4A',10000,'Redmi')
prod2.displayPrdouctDetails()
Output
ProductId:
101
ProductName:
Ipad
ProductPrice:
10000
ProductBrand:
Apple
============================
ProductId:
102
ProductName:
Redmi4A
ProductPrice:
10000
ProductBrand:
Redmi
Post a Comment