Python Classes and objects - Study Notes
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
- Understand the fundamental concepts of classes, objects, constructors, and destructors.
- Learn how to define a class and instantiate it in Python.
- Differentiate between class variables and instance variables.
- Master the usage of constructors to initialize variables and destructors to release resources.
- Understand the difference between public and private data members.
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
- Omitting the self Parameter: A common error is leaving out the
selfparameter in the class method definition. This causes a run-time error when the method is invoked, as Python automatically passes the calling object as the first argument. - Double Underscore Spelling: Students often write
_init_with a single underscore. The constructor must be written with a double underscore on both sides:__init__. - Accessing Private Members Directly: Attempting to access private variables (prefixed with double underscores) from outside the class raises an
AttributeError. Always use public methods to access private data.
Exam Tips
- 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.
- Understand Instantiation Arguments: During object instantiation, never supply an argument for the
selfparameter. Arguments should only be supplied for additional parameters defined afterself. - Identify Object vs Class Reference: Use
self.variableto access instance-specific data andClassName.variableto access class-level shared variables to avoid logic errors.