Python - classes


Classes are defined as a blueprint that encapsulates data and its related operations under a single name.

In this article we will see the scripts related to the following topic.
1) Inheritance
2) static method
3) class method
4) instance variable
5) class variable


Let's move through the topics 1-by-1 with examples.



  """
      created_at       : 13 Feb 2017.
      updated_at       : 13 Feb 2017.
      aim of script    : To understand the concept of classes in python.
      coded by         : Rishikesh Agrawani.
      Python version   : 2.7.12
  """
  

 #Defining a class

class Car: def __init__(self): #This is the constructor of class Car self.name = "Maruti" #Adding name attribute to class instance self.color = "Red" #Adding color attribute to class instance self.price = 1000000 #Adding price attribute to class instance def show_details(self): #This method prints the value of attributes of invoking object print "Name : ",self.name print "Color : ",self.color print "Price : ",self.price

Inheritance is defined a mechanism of deriving a new class from the existing one.
In the following example, ThreeNums & TwoNums are classes & ThreeNums is inheriting TwoNums class.

 #Inheritance

"""class TwoNums""" class TwoNums: def __init__(self,a,b): #TwoNums class constructor self.num1 = a self.num2 = b def add_two_nums(self): #It adds the value of attributes num1 and num2 then returns the sum return self.num1 + self.num2
"""class ThreeNums""" class ThreeNums(TwoNums): def __init__(self,a,b,c): #ThreeNums class counstructor TwoNums.__init__(self,a,b) #Calling parent's constructor to instantiate it self.num3 = c def add_three_nums(self): return (self.add_two_nums()+self.num3) #It returns the sum of attributes num1, num2 and num3 then returns the sum obj = ThreeNums(10,30,60) print obj.add_three_nums() #100

static method is defined as a method which neither takes an instance nor takes class as its first argument.
In the following example, I've defined sum() as static method.

<
  

 #static method

class A: def __init__(self): #constructor self.a = 10 self.b = 11 @staticmethod def sum(obj): #sum() is an static method return obj.a + obj.b print A.sum(A()) #21 instance = A() print instance.sum(instance) #21

class method is defined as a method which exactly takes class as its first argument.
In the following example, I've defined sum() as class method.

  

 #class method

class A: def __init__(self): #constructor self.a = 10 self.b = 11 @classmethod def sum(cls): #sum() is a class method obj = cls() #Instantiation print "cls => ",cls #cls => __main__.A return obj.a + obj.b print A.sum() #21

An instance variable is defined as a variable that is created by the constructor of class.
In the following example, instance and obj are 2 instance variables.

  

 #instance variable

class Numbers: def __init__(self, num1, num2): self.a = num1 self.b = num2 def sum(self): return self.a + self.b obj = Numbers(10, 12) print obj.sum() #22 instance = Numbers(23,77) print instance.sum() #100

An class variable is defined as a variable that is defined outside the method(s) of class. It is shared among all the instances of class.
In the following example, school is a class variable.

  

 #class variable

class Student: school = "Govt. Model hr. sec. school, Pharasgaon(CG)" #school is a class variable def __init__(self, name, rollno, group): self.name = name self.rollno = rollno self.group = group def get_details(self): print "Name : ",self.name print "Roll No : ",self.rollno print "Group : ",self.group print "School : ",self.school student1 = Student("Rajesh Pather", 24, "Mathematics") #student1 is an instance variable student1.get_details() student2 = Student("Baldev Netam",2,"Biology") #student2 is an instance variable student2.get_details()