Nearby lessons

99 of 159

Python - Classes and Objects

📌 What You Will Learn
  • Understand the concept of a class and an object
  • Define a class using the class keyword
  • Create objects of a class using reference variables
  • Use documentation strings to describe a class
  • Identify the types of variables and methods inside a class

Python's Object Oriented Programming (OOPs)

Python supports Object Oriented Programming (OOPs).

In OOPs, the basic building blocks are:

  • Class
  • Object

This page starts by explaining the concept of a Class.

What is Class?

  • In Python, everything is an object.
  • To create objects, we require some Model, Plan, or Blueprint.
  • This Model, Plan, or Blueprint is called a Class.
  • We can write a class to represent the properties (attributes) and actions (behaviour) of an object.
  • Properties are represented by Variables.
  • Actions are represented by Methods.
  • Hence, a class contains both variables and methods.

Class Diagram

                     Class
                       │
              ┌────────┴────────┐
              │                 │
          Variables         Methods
         (Properties)     (Behaviour)
        

Properties and Actions

A class contains two important parts.

Part Represents
Variables Properties (Attributes)
Methods Actions (Behaviour)

How to Define a Class?

We can define a class by using the class keyword.

Syntax

🐍Code Cell
1class className:
2 ''' documentation string '''
3 
4 # Variables
5 
6 # Instance Variables
7 
8 # Static Variables
9 
10 # Local Variables
11 
12 # Methods
13 
14 # Instance Methods
15 
16 # Static Methods
17 
18 # Class Methods
Output
No output captured.

Explanation of the Syntax

1. class

The class keyword is used to define a class.

2. className

This is the name of the class.

By convention, class names begin with a capital letter.

Documentation String

A documentation string represents the description of the class.

Within a Python class, the documentation string is optional.

How to Access the Documentation String?

Python provides two ways to access the documentation string.

Method 1

Use the __doc__ attribute of the class.

🐍Code Cell
1print(Student.__doc__)
Output
No output captured.

Method 2

Use the help() built-in function.

🐍Code Cell
1help(Student)
Output
No output captured.

Example

🐍Code Cell
1class Student:
2 ''' This is student class with required data '''
3 
4print(Student.__doc__)
5help(Student)
Output
No output captured.

Explanation

  • class Student: defines a class named Student.
  • ''' This is student class with required data ''' is the documentation string of the class.
  • print(Student.__doc__) prints the documentation string.
  • help(Student) displays the help information of the class.

Example of a Class

The following example demonstrates a simple Python class.

🐍Code Cell
1class Student:
2 ''' Developed by durga for python demo '''
3 
4 def __init__(self):
5 self.name = 'durga'
6 self.age = 40
7 self.marks = 80
8 
9 def talk(self):
10 print("Hello I am :", self.name)
11 print("My Age is:", self.age)
12 print("My Marks are:", self.marks)
Output
No output captured.

Understanding the Example

The class is named Student. It contains:

  • A documentation string.
  • A constructor (__init__()).
  • Three variables: name, age, and marks.
  • One method: talk().

The talk() method prints the student's information.

Variables in a Class

Within a Python class, we can represent data by using variables.

There are three types of variables:

  1. Instance Variables (Object Level Variables)
  2. Static Variables (Class Level Variables)
  3. Local Variables (Method Level Variables)
Variable Type Description
Instance Variable Belongs to an object. Every object has its own separate copy.
Static (Class) Variable Shared among all objects of the class.
Local Variable Declared inside a method and accessible only within that method.

Methods in a Class

Within a Python class, we can represent operations by using methods.

There are three types of methods:

  1. Instance Methods
  2. Class Methods
  3. Static Methods
Method Type Description
Instance Methods Operate on object data.
Class Methods Operate on class data.
Static Methods General utility methods.

What is Object?

Physical existence of a class is nothing but an object.

We can create any number of objects for a class.

Understanding Object

A class is only a Blueprint or Model.

When we create the real implementation of that blueprint, it is called an Object.

Class (Blueprint)
        │
        ▼
Create Object
        │
        ▼
Real Physical Existence
        

Syntax to Create an Object

🐍Code Cell
1referenceVariable = ClassName()
Output
No output captured.

What is Reference Variable?

The variable which can be used to refer to an object is called a Reference Variable.

By using the reference variable, we can access the properties and methods of the object.

Understanding Reference Variable

              Student Object
                    ▲
                    │
                    │
                    s
        

Here, s is the reference variable and it refers to the Student object.

What Can We Access by Using a Reference Variable?

Member Example
Properties (Variables) s.name
Methods s.talk()

Program: Create an Object and Call a Method

🐍Code Cell
1class Student:
2 
3 def __init__(self, name, rollno, marks):
4 self.name = name
5 self.rollno = rollno
6 self.marks = marks
7 
8 def talk(self):
9 print("Hello My Name is:", self.name)
10 print("My Rollno is:", self.rollno)
11 print("My Marks are:", self.marks)
12 
13 
14s = Student("Durga", 101, 90)
15s.talk()
Output
Hello My Name is: Durga
My Rollno is: 101
My Marks are: 90

Explanation

  • class Student: defines a class named Student.
  • The constructor receives name, rollno, and marks, and stores them inside the object using self.
  • s = Student("Durga", 101, 90) creates a Student object and stores its reference in s.
  • s.talk() calls the talk() method using the reference variable.

Relationship Between Class, Object and Reference Variable

        Class
          │
          ▼
     Student()
          │
          ▼
        Object
          ▲
          │
          │
          s
 (Reference Variable)
        

The class defines the blueprint, Student() creates the object, and the reference variable s is used to access the object.

📝 Key Takeaways
  • A class is a blueprint, and an object is its physical existence
  • A class is defined using the class keyword followed by a class name
  • An object is created with the syntax referenceVariable = ClassName()
  • The documentation string describes the class and is accessed with __doc__ or help()
  • A class can contain instance variables, static variables, local variables, and instance, class, and static methods

🧠 Test Your Knowledge

10 Questions
Progress: 0 / 10