This chapter begins the essential topic of Object-Oriented Programming (OOP), a paradigm focused on creating reusable code using classes and objects. This is a foundational concept for building complex, organized applications.
Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of objects. It aims to model real-world entities by bundling data (attributes) and methods (functions) that operate on that data into single units called objects.
1. Key OOP Concepts
- Class: A blueprint or template for creating objects. It defines the properties (attributes) and behaviors (methods) that all objects created from it will have.
- Object (Instance): A concrete realization of a class. When a class is created, an object is instantiated.
- Encapsulation: The bundling of data (attributes) and the methods that operate on that data into a single unit (the class). This prevents external, unwanted access to data.
- Abstraction: Hiding the complex implementation details and showing only the essential information to the user.
2. Defining Classes
You define a class using the class keyword, followed by the class name (conventionally starting with an uppercase letter).
# The Class is the blueprint
class Dog:
# Class attribute (shared by all instances)
species = "Canis familiaris"
# The __init__ method is the constructor
def __init__(self, name, age):
# Instance attributes (unique to each object)
self.name = name
self.age = age
3. The Constructor (__init__)
The __init__ (pronounced “dunder init”) method is a special method called automatically whenever a new object (instance) is created from the class.
- Purpose: To initialize the instance attributes (data) with the values provided when the object is created.
selfParameter (Crucial):selfis always the first parameter of any instance method in Python, including__init__.- It refers to the instance itself (the object being created/referenced).
- When you write
self.name = name, you are assigning the value passed as an argument (name) to the object’s attribute (self.name).
4. Creating Objects (Instantiation)
To create an object from the Dog class, you call the class name as if it were a function, passing the required arguments for the __init__ method (excluding self).
# Creating two different objects (instances)
dog1 = Dog(name="Buddy", age=5)
dog2 = Dog(name="Luna", age=2)
# Accessing Instance Attributes
print(f"{dog1.name} is {dog1.age} years old.")
# Output: Buddy is 5 years old.
# Accessing the Class Attribute (shared)
print(Dog.species)
# Output: Canis familiaris
5. Instance Methods
Instance methods are functions defined inside a class that operate on the object’s instance attributes. They must also take self as their first parameter.
class Dog:
species = "Canis familiaris"
def __init__(self, name, age):
self.name = name
self.age = age
# Instance Method
def bark(self):
return f"{self.name} says WOOF!"
# Instance Method that uses the instance's own data
def get_human_age(self):
return self.age * 7
dog1 = Dog("Buddy", 5)
print(dog1.bark()) # Calling the method on the object
# Output: Buddy says WOOF!
print(dog1.get_human_age()) # Output: 35
When you call dog1.bark(), Python internally passes the dog1 object as the self argument to the bark method.
