Type of Variables
Type of Variables
print(self.model)
realme = Mobile( ) Accessing Instance Variable
Accessing Instance Variable
Outside Class
We can access instance variable using object_name.variable_name
class Mobile:
def __init__(self):
self.model = ‘RealMe X’ Instance Variable
def show_model(self): Instance Method
print(self.model)
Accessing Instance Variable
realme = Mobile( )
print(realme.model) Accessing Instance Variable from outside Class
Instance Variable
Instance variables are the variables whose separate copy is created in every object.
If we modify the copy of Instance variable in an instance, it will not affect all the
copies in the other instance. Heap Memory
class Mobile:
def __init__(self): Instance Variable self.model = ‘RealMe X’
def show_model(self):
print(self.model) self.model = ‘RealMe X’
realme = Mobile( ) redmi
redmi = Mobile( )
samsung = Mobile( ) self.model = ‘RealMe X’
samsung
Class Variable / Static Variable
Class variables are the variables whose single copy is available to all the instance of the class.
If we modify the copy of class variable in an instance, it will affect all the copies in the other
instance.
Ex:-
class Mobile:
fp = ‘Yes’ Class Variable
def __init__(self):
self.model = ‘RealMe X’
def show_model(self):
print(self.model)
realme = Mobile( )
Accessing Class/Static Variable
With Class Method
To access class variable, we need class methods with cls as first parameter then we can access
class variable using cls.variable_name
class Mobile:
fp = ‘Yes’ Class Variable
def __init__(self):
self.model = ‘RealMe X’
def show_model(self):
print(self.model)
@classmethod Class Method
def is_fp(cls):
cls.fp
Accessing Class Variable inside Class Method
realme = Mobile( )
Accessing Class/Static Variable
Outside Class
We can access class variable using Classname.variable_name
class Mobile:
fp = ‘Yes’ Class Variable
realme = Mobile( )
@classmethod
def is_fp(cls):
realme redmi geek
print(cls.fp)
realme = Mobile( ) Heap Memory
redmi = Mobile( )
geek = Mobile( )
print(Mobile.fp)
Local Variables
• Sometimes to meet temporary requirements of a programmer, we can declare
variables inside a method directly, such type of variables are called local variable or
temporary variables.
• Local variables will be created at the time of method execution and destroyed once
method completes.
• Local variables of a method cannot be accessed from outside of method.