0% found this document useful (0 votes)
18 views10 pages

Type of Variables

The document explains three types of variables in programming: instance variables, static (class) variables, and local variables. Instance variables are unique to each object, static variables are shared across all instances, and local variables exist only during method execution. It also provides examples of how to declare and access these variables within classes and methods.

Uploaded by

classgattapugari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views10 pages

Type of Variables

The document explains three types of variables in programming: instance variables, static (class) variables, and local variables. Instance variables are unique to each object, static variables are shared across all instances, and local variables exist only during method execution. It also provides examples of how to declare and access these variables within classes and methods.

Uploaded by

classgattapugari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Type of Variable

• Instance Variable (Object Level Variables)


• Static Variable (Class Level Variables )
• Local Variable (Method Level Variables)
Instance Variable
Instance variables are the variables whose separate copy is created in every object.
Instance variables are defined and initialized using a constructor with self parameter.
How to declare instance variable
1. Inside Constructor by using self variable
2. Inside Instance Method by using self variable
3. Outside of the class by using object reference variable
Ex:-
class Mobile:
def __init__(self):
self.model = ‘RealMe X’ Instance Variables
def show_model(self):
self.camera=4
print(self.model)
realme = Mobile( )
realme.ram="8gb"
Accessing Instance Variable
We can access instance variables with in the class by using self variable and outside of
the class by using object reference.
With Instance Method
To access instance variable, we need instance methods with self as first parameter then
we can access instance variable using self.variable_name
class Mobile:
def __init__(self):
Instance Variable
self.model = ‘RealMe X’
def show_model(self): Instance Method

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’

self.model = ‘RealMe X’ realme

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

@classmethod Class Method


def show(cls):
cls.fp Accessing Class Variable inside Class Method

realme = Mobile( )

Mobile.fp Accessing Class Variable outside class


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 effect all the copies in the other
instance.
class Mobile:
fp = ‘Yes’ fp = ‘Yes’

@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.

You might also like