Mastering Python Objects & Classes: A Practical Guide
Python Objects and Classes
This tutorial explores Python’s core object‑oriented features, guiding you through class creation, instantiation, and best practices for robust code.
Video: Python Classes and Objects
Python Objects and Classes
Python is fundamentally object‑oriented. While procedural programming focuses on functions, OOP centers on objects that bundle data and behavior.
An object is a bundle of attributes (data) and methods (functions). A class is the blueprint that defines an object’s structure.
Think of a class as a house blueprint—detailing rooms, doors, windows. Each constructed house is an object, or instance, created from that blueprint.
Multiple objects can be built from the same class. Instantiation is the process of creating an object from a class.
Defining a Class in Python
Class definitions start with the class keyword, mirroring how functions begin with def. The first string in a class is the docstring, providing a concise description.
class MyNewClass:
'''This is a docstring. I have created a new class'''
pass
Defining a class establishes a new namespace for its attributes, which can be data or methods. Special attributes beginning with double underscores, such as __doc__, hold metadata like the docstring.
Once defined, a class object is created and can be used to access attributes or instantiate new objects.
class Person:
"This is a person class"
age = 10
def greet(self):
print('Hello')
# Output: 10
print(Person.age)
# Output: <function Person.greet>
print(Person.greet)
# Output: "This is a person class"
print(Person.__doc__)
Output
10 <function Person.greet at 0x7fc78c6e8160> This is a person class
Creating an Object in Python
Classes serve as factories for objects. Instantiation resembles a function call:
>>> harry = Person()
The resulting object harry can access class attributes via dot notation. Class methods become bound methods on the instance.
class Person:
"This is a person class"
age = 10
def greet(self):
print('Hello')
# create a new object of Person class
harry = Person()
# Output: <function Person.greet>
print(Person.greet)
# Output: <bound method Person.greet of <__main__.Person object>>
print(harry.greet)
# Calling object's greet() method
# Output: Hello
harry.greet()
Output
<function Person.greet at 0x7fd288e4e160> <bound method Person.greet of <__main__.Person object at 0x7fd288e9fa30>> Hello
Notice the self parameter in method definitions. When a method is called on an instance, Python automatically passes the instance as the first argument, enabling harry.greet() to execute without explicitly providing self.
By convention, self is used, though any name is syntactically valid. Following this convention enhances readability and aligns with community standards.
Constructors in Python
Special methods prefixed with double underscores perform special roles. The __init__ method, invoked upon instantiation, acts as the constructor, initializing instance state.
class ComplexNumber:
def __init__(self, r=0, i=0):
self.real = r
self.imag = i
def get_data(self):
print(f'{self.real}+{self.imag}j')
# Create a new ComplexNumber object
num1 = ComplexNumber(2, 3)
# Call get_data() method
# Output: 2+3j
num1.get_data()
# Create another ComplexNumber object
# and add a dynamic attribute 'attr'
num2 = ComplexNumber(5)
num2.attr = 10
# Output: (5, 0, 10)
print((num2.real, num2.imag, num2.attr))
# Accessing a missing attribute raises an error
print(num1.attr)
Output
2+3j
(5, 0, 10)
Traceback (most recent call last):
File "<string>", line 27, in <module>
print(num1.attr)
AttributeError: 'ComplexNumber' object has no attribute 'attr'
This example demonstrates that attributes can be added dynamically to individual instances without affecting others.
Deleting Attributes and Objects
Attributes can be removed with the del statement. Deleting the class method or attribute affects subsequent calls.
>>> num1 = ComplexNumber(2,3)
>>> del num1.imag
>>> num1.get_data()
Traceback (most recent call last):
...
AttributeError: 'ComplexNumber' object has no attribute 'imag'
>>> del ComplexNumber.get_data
>>> num1.get_data()
Traceback (most recent call last):
...
AttributeError: 'ComplexNumber' object has no attribute 'get_data'
Objects themselves can be deleted. Removing the reference frees the name; if no other references exist, the object is garbage‑collected automatically.
>>> c1 = ComplexNumber(1,3)
>>> del c1
>>> c1
Traceback (most recent call last):
...
NameError: name 'c1' is not defined
In Python, memory management is handled by reference counting and a cyclic garbage collector, ensuring efficient resource cleanup.

Python
- C++ Classes & Objects: A Practical Guide to Object‑Oriented Programming
- C++ Friend Functions and Friend Classes: Mastering Access Control
- Python Namespaces & Variable Scope: Understanding Names, Bindings, and Scopes
- Mastering Python Custom Exceptions: A Practical Guide
- Mastering Python Inheritance: Concepts, Syntax, and Practical Examples
- Java Classes and Objects: A Practical Guide
- Python OOP Fundamentals: Classes, Objects, Inheritance, and Constructors Explained
- Understanding type() and isinstance() in Python: Practical Examples
- Mastering Python Class Slots: Optimize Memory & Speed
- Master Java: Understanding Objects, Classes, and Core OOP Concepts