TN Online TestSamacheer Kalvi practice

Python Classes and objects - Study Notes

Share this chapter: Telegram

Chapter Summary

Python is an object-oriented programming language where classes serve as the templates or blueprints for creating objects. An object is a collection of bundled data and functions that operate on that data. In Python, everything is treated as an object, including standard integers and strings. This chapter covers the creation of classes, object instantiation, resource management using constructors and destructors, and access control mechanism where members are public by default unless explicitly prefixed with a double underscore to make them private.

Learning Objectives

Key Concepts and Definitions

Class

A class is a user-defined blueprint or prototype that defines the attributes (variables) and methods (functions) shared by all objects of its type.

Object

An object is an instance of a class that combines data and methods. Every object has a distinct state and behavior defined by its class template.

Methods

Methods are functions defined inside a class block that are subordinate to the class and are used to manipulate or analyze the data members.

Self Parameter

A special parameter that refers to the specific instance of the class calling the method. It must be defined as the first parameter of any class method, though it is not passed explicitly during the call.

Constructor

A special method named __init__() that automatically runs when a new object of the class is created. It is primarily used to declare and initialize instance variables.

Destructor

A special method named __del__() that automatically executes when an object goes out of scope or is deleted using the del statement, releasing memory.

Public and Private Members

By default, all data members in a Python class are public and can be accessed from outside the class environment. Prefixing a variable name with a double underscore makes it private, restricting its access to within the class methods only.

Worked Methods

Declaring a Class and Instantiating Objects

To declare a class, use the class keyword followed by the class name and a colon. Inside the class, you can define variables and methods. Methods must always have self as their first parameter. To instantiate the class, call the class name like a function and assign it to a variable.

For example, if you define a class named Sample with variables x and y, you instantiate it using S = Sample() and access the variables using the dot operator S.x and S.y.

Implementing a Constructor and Class Variable

To keep track of the number of objects created, you can declare a class variable outside any method. Inside the constructor __init__(), increment the class variable using the class name prefix, and assign the individual instance variable using the self keyword. When objects are instantiated, the class variable value is shared across all instances, maintaining a global counter.

Common Exam Traps

Exam Tips

  1. Remember Default Accessibility: In Python, class members are public by default, which is the exact opposite of C++ and Java where they are private by default.
  2. Understand Instantiation Arguments: During object instantiation, never supply an argument for the self parameter. Arguments should only be supplied for additional parameters defined after self.
  3. Identify Object vs Class Reference: Use self.variable to access instance-specific data and ClassName.variable to access class-level shared variables to avoid logic errors.
Solved MCQs → Practice test →

More for this chapter

Book Back Questions10 textbook MCQs · solved Additional MCQs15 extra MCQs · solved Practice TestInteractive · instant score Book Back TestTest yourself on the textbook set Additional MCQ TestTest yourself on the extra set Formula SheetAll key formulas

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