Classes and Objects in Python
Chapter 10 · Tamil Nadu State Board
- 6 min read
- 5-question quiz
Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content
In short
This lesson explains how Python classes act as templates for objects and how to create objects to use class members. It shows how methods, class variables, and instance variables work, including the role of self. It also teaches how constructors initialize objects and how destructors are used when objects are deleted. These ideas help students organize data and functions into Python programs.
Written by AI from this lesson. How we check it
Key points
- A class is a template for objects, and an object is a collection of data and functions that act on that data.
From your textbook
“Object is a collection of data and function that act on those data. Class is a template for the object.”
- Python classes are declared with the class keyword, and functions defined inside a class are called methods.
From your textbook
“In Python, a class is defined by using the keyword class.”
- Creating an object from a class is called class instantiation.
From your textbook
“The process of creating object is called as “Class Instantiation”.”
- An object uses the dot operator to access a class variable or method.
From your textbook
“Any class member ie. class variable or method (function) can be accessed by using object with a dot ( . ) operator.”
- A method uses self as its first parameter, and Python supplies its value when the method is called.
From your textbook
“Python provides its value automatically.”
- A constructor runs automatically when an object is created and can initialize class variables.
From your textbook
“This method is used to initialize the class variables.”
- A double underscore before a variable makes it private, so it can be accessed only within the class.
From your textbook
“These variables can be accessed only within the class.”
Formulas in this chapter
- Creating an objectObject_name = class_name()Explained here
- Accessing a class memberObject_name.class_memberExplained here
- Constructor formatdef __init__(self, [args ……..]):Explained here
- Circle calculations used in the programarea = Circle.pi * (self.radius ** 2); circumference = 2 * Circle.pi * self.radiusExplained here
Classes and Objects in Python
A class is a template for creating objects. An object is a collection of data and functions that work on that data. An object is also called an instance of a class.
Definition
- Class
- A class is a template that groups variables and methods for creating objects.
Definition
- Object
- An object is an instance of a class that contains data and functions.
Class members
- Variables defined inside a class are called class variables.
- Functions defined inside a class are called methods.
- Class variables and methods together are called members of the class.
Define a class with the keyword class, followed by a unique class name and a colon. The statements inside the class must be properly indented. A class can be defined anywhere in a Python program.
Object_name = class_name()
Object_name is the name given to the object, and class_name is the class being instantiated.
Object_name.class_member
Use the object name, a dot, and the name of the class variable or method.
Class Methods and Parameters
A class method must have self as its first parameter, even when it takes no other arguments. Python supplies a value for self automatically when the method is called through an object. A method that accepts one defined parameter receives self and that parameter.
Definition
- Parameter
- A parameter is a variable written in a function definition.
Definition
- Argument
- An argument is a value passed to a function.
To use a class variable inside a method, prefix it with the class name and a dot. In the Student example, process adds mark1, mark2 and mark3, then finds and displays their average.
Worked example
Find the total and average of marks 45, 91 and 71 using a class method.
Try it first, then show the solutionSolution
- Add the three marks: 45 + 91 + 71 = 207.
- Divide the total by 3 to find the average.
Answer: Total Marks = 207; Average Marks = 69.0
A method can also receive a value through an argument. In the Odd_Even example, check(self, num) tests whether num%2==0 and prints whether the number is even or odd.
Odd and even results
| Input | Output |
|---|---|
| 4 | 4 is Even number |
| 5 | 5 is Odd number |
Constructors and Instance Variables
Definition
- Constructor
- A constructor is a special function that runs automatically when an object of a class is created.
Python uses __init__ as the constructor. It begins and ends with double underscores, and it can be defined with or without arguments. Constructors are used to initialize class variables.
def __init__(self, [args ……..]):
self refers to the object, and args represents optional arguments.
In the Sample example, creating S with Sample(10) runs the constructor automatically. It prints a message, stores 10 in self.num, and prints the value.
Definition
- Instance variable
- An instance variable is a variable declared inside a method with self, and each object has its own separate copy.
Class variables and instance variables
| Class variable | Instance variable |
|---|---|
| Shared by objects of the class. | Has a separate copy for each object. |
| A change is reflected for all objects that share it. | Its value can vary from object to object. |
In the object-count example, Sample.num starts at 0 and increases by 1 whenever an object is created. The three objects have values 15, 35 and 45, and the displayed count goes from 1 to 3.
Destructor and Public Data Members
Definition
- Destructor
- A destructor is a special method used to destroy objects.
Python uses __del__() as a destructor. It is the opposite of a constructor. The __del__ method is called automatically when the object reference is deleted using del.
Variables defined inside a class are public by default. They can be accessed anywhere in the program using the dot operator.
Remember: Remember: __init__ runs when an object is created, while __del__ is called when its reference is deleted using del.
Private and Public Class Variables
Remember: A double underscore makes a class variable private, so access it through a method inside the class.
Circle Area and Circumference Program
area = Circle.pi * (self.radius ** 2); circumference = 2 * Circle.pi * self.radius
Circle.pi is 3.14, and self.radius is the radius stored in the object.
Worked example
The program is given radius 5. Find the area and circumference shown by its output.
Try it first, then show the solutionSolution
- The area is 3.14 * (5 ** 2).
- The circumference is 2 * 3.14 * 5.
Answer: The Area = 78.5. The Circumference = 31.400000000000002.
Counting Characters in a String
What the count() method checks
- isupper() checks for uppercase letters.
- islower() checks for lowercase letters.
- Membership in AEIOUaeiou checks for vowels.
- isspace() checks for spaces.
- Consonants are counted as uppercase letters plus lowercase letters minus vowels.
Worked example
The program receives the string Welcome To Learn Computer Science. What counts does it display?
Try it first, then show the solutionSolution
- The program checks each character and counts uppercase letters, lowercase letters, vowels and spaces.
- It calculates consonants by subtracting vowels from the total uppercase and lowercase letters.
Answer: 4 Uppercase letters, 25 Lowercase letters, 12 Vowels, 13 Consonants and 4 Spaces.
Classes, Objects, Constructors and Destructors
Constructor and destructor
| Constructor | Destructor |
|---|---|
| Runs automatically when an object is created. | Runs automatically when an object exits its scope. |
| Python method: __init__. | Python method: __del__. |
Remember: Remember the key terms: class, object, method, instantiation, __init__ and __del__.
Questions students ask
Short, direct answers to what students search for in this chapter.
What is a class in Python?
A class is a template for creating objects. It can contain variables and functions. Variables defined in a class are called class variables, and functions defined in it are called methods. Together, these variables and methods are the members of the class.
How do you create an object in Python?
Create an object by writing Object_name = class_name(). This process is called class instantiation. After creating the object, use its name and a dot to access class members, such as Object_name.class_member.
Why is self used in a Python class method?
self is the required first parameter in a Python class method. Python supplies its value automatically when the method is called through an object, so you do not pass a value for self. It is used to refer to the object and to declare instance variables.
What is the difference between a class variable and an instance variable?
A class variable is shared by objects of the class, so a change to it is reflected across those objects. An instance variable is declared inside a method using self. Each object has a separate copy, so its value can vary from object to object.
What do __init__ and __del__ do in Python?
__init__ is the constructor and runs automatically when an object is created. It can initialize variables. __del__ is the destructor. It is called automatically when an object reference is deleted using del.
What is a private variable in Python?
A private class variable has a name prefixed with double underscores, such as __n2. It can be accessed by methods inside its class. Trying to access it directly from outside the class causes an AttributeError.
Show 3 more questions
How does the String class count consonants?
The String class adds the uppercase and lowercase letter counts, then subtracts the vowel count. In its program, consonants are calculated as self.upper + self.lower - self.vowel.
What is instantiation in Python?
Instantiation is the process of creating an object from a class. When an object is created, the class constructor runs automatically. Python uses the __init__ method as its constructor.
What is the difference between a constructor and a destructor in Python?
A constructor runs automatically when an object is created. Python uses __init__ as the constructor. A destructor runs automatically when an object exits its scope, and Python uses __del__ as the destructor.
Book back answers
21 textbook exercises, solved and checked against the book. Questions we could not prove are left out.
Choose the best answer (1 Mark)
Which of the following are the key features of an Object Oriented Programming language?
- (a) Constructor and Classes
- (b) Constructor and Object
- (c) Classes and Objects
- (d) Constructor and Destructor
Try it, then show the answerAnswer
(c) Classes and Objects
The chapter identifies classes and objects as key features of Object Oriented Programming.
From the book: “Classes and Objects are the key features of Object Oriented Programming.”
Functions defined inside a class:
- (a) Functions
- (b) Module
- (c) Methods
- (d) section
Try it, then show the answerAnswer
(c) Methods
Functions defined inside a class are called methods.
From the book: “Variables defined inside a class are called as “Class Variable” and functions are called as “Methods”.”
Class members are accessed through which operator?
- (a) &
- (b) .
- (c) #
- (d) %
Try it, then show the answerAnswer
(b) .
The dot operator is used with an object to access a class member.
From the book: “Any class member ie. class variable or method (function) can be accessed by using object with a dot ( . ) operator.”
Which of the following method is automatically executed when an object is created?
- (a) __object__( )
- (b) __del__( )
- (c) __func__( )
- (d) __init__( )
Try it, then show the answerAnswer
(d) __init__( )
The constructor is executed automatically when an object is created, and the chapter identifies __init__ as the constructor.
From the book: “In Python, there is a special function called “init” which act as a Constructor.”
Practice
What does class instantiation mean?
Exam questions
Practice questions with model answers. These are not from an official paper.
What is a class, and what is an object?2 marks
A class is a template for an object. An object is a collection of data and functions that act on that data.
What is class instantiation?2 marks
Class instantiation is the process of creating an object from a class.
What is the purpose of a constructor in Python?2 marks
A constructor runs automatically when an object is created and is used to initialize class variables.
How are private variables identified, and where can they be accessed?2 marks
A variable prefixed with a double underscore is private. It can be accessed only within the class.
Watch out
- Do not confuse a class variable, which is shared by objects, with an instance variable, which has a separate copy for each object.
- Do not try to access a private variable from outside its class.
Still stuck on this lesson?
Get an AI answer grounded in your actual textbook — with the exact page reference.
Ask AI about this topic →