Types of Variables & Methods in Python OOPs
Introduction
In this blog we will focus on different types of Variables & Methods in Python OOPs before starting the blog you must be clear in the basic concepts of OOPs https://ainewgeneration.com/tag/oops/.
Table of Content
- Types of Variables in OOPs.
- Instace Variable.
- Class or Static Variable.
- Types of Methods in OOPs.
- Instance methods.
- Class methods.
- Static methods.
Type of Variables in OOPs
There are 2 types of variables in OOPs i.e.:
- Instance Variables.
- Class or Static Variables.
Instance Variables
class Computer:
def __init__(self):
#instance variable
self.cpu = "i5"
self.ram = "8GB"
In the above code cpu & ram, they are instance variables as your computer changes i.e. the object changes the values also get changes. By default, the value is i5 and 8GB but you can change it by accessing the variable name inside the class. As you can see below instance variables are different for different objects.
Instance variables can be accessed directly by calling the instance variable name inside the class.
#makes objects of Computer class
comp_1 = Computer()
comp_2 = Computer()
comp_3 = Computer()
#chnage varibales for different object
comp_1.cpu = "i7"
comp_2.ram = "16GB"
print(comp_1.cpu ,comp_1.ram)
print(comp_2.cpu ,comp_2.ram)
print(comp_3.cpu ,comp_3.ram)
output : i7 8GB
i5 16GB
i5 8GB
Class or Static Variable
The class or static Variable is the same for all objects in other ways the variable which gets changes will affect all the objects known as a class or static variable.
The class or static variables are defined outside the methods whereas the instance variable is defined inside the methods.
class Computer:
#static or class variable
gpu = "4GB"
def __init__(self):
#instance variable
self.cpu = "i5"
self.ram = "8GB"
#makes objects of Computer class
comp_1 = Computer()
comp_2 = Computer()
comp_3 = Computer()
#chnage varibales for different object
comp_1.cpu = "i7"
comp_2.ram = "16GB"
print(comp_1.cpu ,car_1.ram,comp_1.gpu)
print(comp_2.cpu ,car_2.ram,comp_2.gpu)
print(comp_3.cpu ,car_3.ram,comp_3.gpu)
output : i7 8GB 4GB
i5 16GB 4GB
i5 8GB 4GB
In the above code class or static variable are same for every object so when the static variable is changed it effect the all the object.
In your memory you have a different namespace it is defined as where you can create and store the objects and variables. There are 2 types of the namespace :
- class/static namespace : where you can store all the class variable in namespace.
- instace namespace : where you can store all the instace variable in namespace.
Types of Methods in OOPs
The function in object-oriented programming is called methods Where you can define the behavior. There are 3 types of methods in oops.
- Instance methods.
- Class methods.
- Static methods.
Instance methods
For instance method return different outputs for different objects as a self parameter is passed to it means it belongs to a particular object and the Instance method is used with the instance variables.
Instance methods are of 2 types :
- Accessor Methods (get methods): If you want to featch the value for instance varibale you will be using accessor method .
- Mutator Metods (set methods) : If you want to modify the values you will be using mutator method.
Class methods
The class methods return the same output for different objects as cls parameters are passed to class methods any changes in the class variable will reflect changes in all the objects as It works only with the class variable.
When you are defining class method you used to define a special symbol called as Decorators which simply modify behaviors or functionality of methods i.e. @classmethod.
Static methods
The static method does not work with any of two variables i.e. instance variables or class variables. which is not concerns with any of the two variables. To defines a static method you need to define a decorator i.e. @staticmethod.
class Student:
school = "D.P.S Public School"
def __init__(self, phy ,chem ,maths):
self.phy = phy
self.chem = chem
self.maths = maths
# Instance Method
def avg_marks(self):
return (self.phy + self.chem+ self.maths)/3
# Accessor Method
def get_phy(self):
return self.value
# Mutator Method
def set_phy(self,value):
self.phy = value
# Class Method
@classmethod
def get_school_name(cls):
return cls.school
# Static Method
@staticmethod
def info():
retrurn "This is High School"
# student 1
std_1 = Student(91,88,90)
# student 2
std_2 = Student(80,90,75)
print("student 1 :",std_1.avg_marks())
print("student 2 :",std_2.avg_marks())
# Invoking class method
Student.get_school_name()
# Invoking static method
Student.info()
output: student 1 : 89.66
student 2 : 81.66
D.P.S Public School
This is High School
In the above code, avg_marks is an instance method as self parameter is passed to it means it belongs to a particular object. As instance method return different output for different objects like for std_1 and std_2. Similar to the class & static method it returns the output which will be the same for all objects.
End Notes
I hope this blog gives you a clear idea about types of variables and methods in python oops. In the next blog, we will go on concepts of inheritance in Python.