TN Online TestSamacheer Kalvi practice

Python Classes and Objects: One Mark Questions, Book Back Answers and Notes

12th Standard · Computer Science · #10

This chapter introduces object-oriented programming in Python through classes and objects. It covers defining a class, creating objects, writing class methods with the self parameter, using the constructor __init__() and destructor __del__(), and making data members public or private with a double underscore prefix.

Study this chapter

Asked in previous board exams

8 questions from this chapter came in 7 past public exams. Open any of them to see the answer and explanation.

  1. March 2026
    Which of the following is the private class variable?
    See answer in the solved paper →
  2. March 2025
    Class members are accessed through which operator?
    See answer in the solved paper →
  3. March 2024
    In Python the process of creating an object is called as ________.
    See answer in the solved paper →
  4. March 2023
    __________ members are accessible from outside the class.
    See answer in the solved paper →
  5. March 2023
    Class members are accessed through __________ operator.
    See answer in the solved paper →
  6. May 2022
    Which of the following method is used as destructor ?
    See answer in the solved paper →
  7. Supplementary 2021
    The process of creating an object is called as _________.
    See answer in the solved paper →
  8. March 2020
    Class members are accessed through which operator ?
    See answer in the solved paper →

About Python Classes and Objects

Medium ~90 min study

Python is an object-oriented language. In earlier chapters you wrote separate variables and functions. Here you learn to put the data about something, such as a student's name and marks, together with the functions that work on that data, in one unit called a class.

The ideas follow a clear order. A class is the template; its variables and functions are together called class members. An object is made from the class by instantiation, and each object holds its own values. Methods use the self parameter to reach the data of the object that called them. Two special methods run automatically: the constructor __init__() when an object is created, and the destructor __del__() when it is destroyed.

Be ready to write a small class with a constructor, access members with the dot operator, name the first parameter of a method, or say what happens when a private variable such as __salary is accessed from outside the class. Write out one complete example class and object from memory before the exam.

What you'll learn

  • Define a class and create objects from it.
  • Write methods with self as the first parameter and call them through an object.
  • Use the constructor __init__() to give initial values to an object's variables.
  • Explain when the destructor __del__() runs.
  • Declare public and private data members and access public ones with the dot operator.
  • Find and correct errors caused by accessing private members from outside a class.

Before you start

  • Python variables, functions and control structures.
  • Writing and calling functions with parameters.
  • The idea of dividing a program into reusable parts.

Topics covered in this chapter

Class A template, defined with the class keyword, that sets out the variables and methods its objects will have.
Object An instance of a class holding its own data values and able to use the methods defined in the class.
Instantiation Creating an object from a class using function-call notation, such as S = Sample().
Class Methods Functions defined inside a class that describe what an object can do; each takes self as its first parameter.
The self Parameter The first parameter of a method, which refers to the object that called it and is passed by Python automatically.
Constructor __init__() A special method that runs automatically when an object is created, used to set the object's initial values.
Private Data Members Variables whose names start with two underscores, usable inside the class but giving an AttributeError when accessed directly from outside.

Python Classes and Objects explained

Classes and Objects in Python

What Classes and Objects Are

Object-oriented programming builds a program around objects that stand for real things, such as a student or a bank account. A class is the template that says what data an object will hold and what it can do. An object is one actual instance made from that class, with its own values. Many objects can be made from one class, and all of them share its methods. In Python, even values like integers and strings are objects of built-in classes such as int and str.

Defining a Class and Creating Objects

A class is defined with the class keyword, the class name and a colon. The indented body contains variables and function definitions. For example, class Sample: followed by x, y = 10, 20 defines a class with two variables. Creating an object is called instantiation and uses function-call notation: S = Sample(). Members are then reached with the dot operator, as in S.x.

Class Methods and self

Functions defined inside a class are called methods. The first parameter of every such method must be self. It refers to the object that called the method, so self.x means the x belonging to that particular object. When you call S.display(), Python passes S as self automatically; you do not write it in the call. If the method has other parameters, you pass only those.

Constructor and Destructor

The constructor is the special method __init__(), with two underscores on each side. It runs automatically when an object is created and is normally used to give initial values to the object's variables, for example def __init__(self, num): self.num = num, called as S = Sample(10). The destructor is __del__(). It runs automatically when the object is about to be destroyed, that is, when no reference to it remains, for example after del S removes the only name that refers to it. It is used for clean-up work.

Public and Private Data Members

Python does not use keywords like public or private. By default every variable and method in a class is public and can be accessed from outside the class with the dot operator. A name that begins with two underscores, such as __n2, is private. Inside the class it is used normally as self.__n2, but writing S.__n2 outside the class raises an AttributeError. Python does this by internally renaming the variable, so private data is protected from accidental use rather than locked completely.

Common mistakes to avoid

  • Leaving out self as the first parameter of a method. Calling that method through an object then gives a TypeError, because Python still passes the object.
  • Accessing a private variable such as S.__n2 from outside the class. Read or change it through a method of the class instead.
  • Writing _init_ or init instead of __init__. With a single underscore or none, Python does not treat it as a constructor and it will not run automatically.
  • Passing a value for self in the call, as in S.display(S). Python supplies self on its own; pass only the other arguments.
  • Using a Python keyword such as class or def as a class or method name, which gives a SyntaxError.

Test yourself on these with the practice test, then check the worked reasoning in the solved MCQs.

Frequently asked questions

What is the difference between a class and an object?

A class is the template that lists the variables and methods. An object is one actual instance created from it, holding real values. From class Student you can create objects s1 and s2, each with its own name and marks but the same methods.

Why is the self parameter needed in methods?

One method is shared by all objects of a class, so it needs to know which object called it. self refers to that object, letting the method use its own variables, such as self.marks. Python passes the calling object as self automatically, so you do not write it in the call.

What does __init__() do?

__init__() is the constructor. It runs automatically each time an object is created and is used to set the object's starting values. For example, with def __init__(self, name): self.name = name, the statement s1 = Student("Anbu") creates an object whose name is Anbu.

How do I make a variable private in a Python class?

Start its name with two underscores, such as __salary. Methods of the class can use it as self.__salary, but accessing obj.__salary from outside raises an AttributeError. Python renames such variables internally, which keeps them from being used by mistake outside the class.

What is a destructor and when does it run?

The destructor is the special method __del__(). It runs automatically when an object is about to be destroyed, which happens when no variable refers to it any more, for example after del removes its only reference, or when the program finishes. It is used for clean-up tasks.

Can a constructor take arguments?

Yes. After self, __init__() can have any number of parameters. The values are given when the object is created, as in S = Sample(10, 20), and are usually stored in the object's variables inside the constructor.

Are class members public by default in Python?

Yes. Every variable and method in a Python class is public unless its name starts with two underscores, so it can be reached from outside with an object and the dot operator. In C++, by contrast, class members are private by default.

Last updated 15 September 2026

More chapters in Computer Science

View all
1 Function 2 Data Abstraction 3 Scoping 4 Algorithmic Strategies 5 Python Variables and Operators 6 Control Structures 7 Python Functions 8 Strings and String Manipulation 9 Lists, Tuples, Sets and Dictionary 11 Database Concepts 12 Structured Query Language (SQL) 13 Python and CSV Files 14 Importing C++ Programs in Python 15 Data Manipulation through SQL 16 Data Visualization using Pyplot: Line Chart, Pie Chart and Bar Chart