Python Classes

Python Classes

We will begin to dive into the concept of object-oriented programming in this chapter. We have already discussed the idea of Object-Oriented programming in our first chapter. For those of you visiting our blog for the first time, we would request to go through the first chapter to understand the concept of Object-Oriented programming in general terms. In this post, we will discuss Python Classes.


Link to all Python chapters here: Learn Python


Python Classes

If you are already familiar with other programming languages like Java or .Net, then you probably know that class is more like a model or plan that one uses to create objects. 

We can use classes to declare attributes and the actions of objects. While variables represent the attributes, the method in a class contains the logic that describes the actions of objects. 

If you wonder what methods are, they are simply functions written inside a class.

How to create Python Classes?

We create a class in Python using the class keyword, followed by the name of the class. Please refer to the block of code below to know the blueprint of the classes.

class Employee:
  #this method is the constructor of the class
  #it is where usually the attributes are declared
  #attributes are also assigned here with values
  def __init__(self):
    self.id = 1234
    self.name = 'Kohli'
    self.title = 'Batter'
  
  #the block below is a method
  #it refers to action of displaying the attributes
  def batting(self):
    print("My name is: ",self.name)
    print("My title is: ",self.title)
    print("My ID is: ",self.id)

You can notice that we declared the class Employee using the keyword class before it. In Python, all classes are derived from the base object class. It means that you can also declare the same class as ‘class Employee(object):’.

The functions declared inside a class are called methods. The first method, def __init__(self), is a constructor, and it is where we usually declare the attributes or assign values to the attributes. This method is defined internally inside a class and cannot be called explicitly. This class also has a second batting method that displays the declared attributes. Displaying the attribute values is the action of this method as per the logic added within it using the print statements. The methods other than the constructor usually have a purpose that defines an action.

You can notice that we used a parameter called ‘self’ after the name of the methods. The ‘self’ refers to the current instance of a particular class. It means that whenever you create a new instance for the Employee class, a different memory block is assigned on the heap, and its location is stored by default in ‘self’. To refer to the instance variables, you can notice that we used the self keyword again, like self.id, self.name, and self.title.

The method batting also takes ‘self as a parameter. The methods used on instances of a particular class are called instance methods. Every Instance method takes ‘self’ as the first parameter that indicates the memory location of that instance.  

We use the following syntax to create an instance for the Employee class. Please note that ‘e1’ in the syntax below is the name of the instance we will create for the Employee class.

Creating an instance, as mentioned above, will generate a block of memory for the instance on the heap. Once the memory allocation is completed, it calls the ‘__init__(self)’ method internally. Eventually, the assigned memory location is returned to the ‘e1’. If you would like to find the memory location of e1, please use id(e1). 

Since ‘e1’ now is the instance of the Employee class, you can refer to the methods or variables of the Employee class as mentioned below.

e1.id 
e1.name 
e1.title 
e1.batting() 

Let us look at a program, create an object for it and call a method from the class using the created instance/object.

class Teams:
  #constructor method
  def __init__(self):
    self.name = 'Chennai Super Kings'
    self.captain = 'MS Dhoni'
    self.teamruns = '10000'
  
  #instance method
  def teaminfo(self):
    print("The name of the team is: ",self.name)
    print("Team's captain is: ",self.captain)
    print("Team has scored {} runs".format(self.teamruns))

t1 = Teams() #creating the instance to Teams class
t1.teaminfo() #calling the method using the instance
Output

The name of the team is:  Chennai Super Kings 
Team's captain is:  MS Dhoni 
Team has scored 10000 runs 

Important Python Classes points to be noted:

  • We create a class using the class keyword 
  • We create the constructor method using ‘__init__(self)’ 
  • The self keyword refers to the instance of a particular class. It means that you can use a self keyword to refer to all instance methods and instance variables.

Python Nested Classes

Just as the name sounds, the Python Nested Classes means a class within another class in a Python program. 

#Python Nested Classes
class Student:
  def __init__(self):
    self.fullname = 'Raju'
    self.dob = self.DateOfBirth()
  def display(self):
    print("Name: ",self.fullname)
  #inner class
  class DateOfBirth:
    def __init__(self):
      self.day = 7
      self.month = 5
      self.year = 1988
    def display(self):
      print('Date of birth = {}/{}/{}'.format(self.day,self.month,self.year))
#outer class object
student1 = Student()
student1.display()
#inner class object
d_o_b = student1.dob
d_o_b.display()
Output

Name:  Raju
Date of birth = 7/5/1988

In the program above, DateOfBirth is the inner class and Student is the outer class.

Scroll to Top