Nearby lessons
99 of 159Python - Classes and Objects
- 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
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.
Method 2
Use the help() built-in function.
Example
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.
Understanding the Example
The class is named Student. It contains:
- A documentation string.
- A constructor (
__init__()). - Three variables:
name,age, andmarks. - 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:
- Instance Variables (Object Level Variables)
- Static Variables (Class Level Variables)
- 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:
- Instance Methods
- Class Methods
- 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
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
Explanation
class Student:defines a class named Student.- The constructor receives
name,rollno, andmarks, and stores them inside the object usingself. s = Student("Durga", 101, 90)creates a Student object and stores its reference ins.s.talk()calls thetalk()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.
- 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