Introduction
In this lesson, we dive into the foundational concepts of object-oriented programming (OOP) in Python, focusing on classes, objects, and attributes. Building upon your understanding of Python from previous lessons, this session aims to equip you with essential skills for designing and implementing well-structured, reusable code using OOP principles.
Defining Python Classes
A class in Python is a blueprint for creating objects. It allows you to define the properties (attributes) and behaviors (methods) relevant to the objects created from the class.
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
Constructors and Attributes
The __init__ method is the constructor method in Python, enabling us to initialize the instance attributes. Here, make, model, and year are instance attributes specific to each Car object.
Creating Objects
Once a class is defined, you can instantiate objects from it. Each object represents a unique instance of that class with its distinct state.
my_car = Car('Toyota', 'Corolla', 2020)
Object Identity and State
Each object has its own identity, which you can check using the id() function, and its state determined by its attribute values.
print(id(my_car)) # Unique identity
Differentiating Attributes
Instance Attributes
Attributes defined within methods (usually in __init__) and prefixed with self are instance attributes.
Class Attributes
These are attributes that are shared among all instances of the class, often defined directly within the class body.
class Car:
wheels = 4 # Class attribute
def __init__(self, make, model, year):
self.make = make # Instance attribute
self.model = model
self.year = year
Local Variables
Local variables are defined within methods and are only accessible within that method.
Using self
The self keyword in Python is used within methods to refer to the current instance of the class, allowing access and modification of the object’s attributes.
class Car:
def display_info(self):
print(f"Car: {self.make} {self.model}, Year: {self.year}")
my_car.display_info() # Accessing attributes via self
Encapsulation in Class Design
Encapsulation involves bundling related data and methods within a class, exposing a clean, functional interface. It helps protect the internal state of an object from unintended external interference.
By keeping some data private (using naming conventions like a leading underscore), a class maintains control over how and when properties are changed or accessed, thus safeguarding data integrity.
class Car:
def __init__(self, make, model, year):
self._make = make # Encapsulated
self.model = model
self.year = year
def get_make(self):
return self._make
my_car = Car('Toyota', 'Corolla', 2020)
print(my_car.get_make())
// Output
Toyota
Summary / Key Takeaways
- Python Classes: Define classes using constructors, attributes, and methods to serve as blueprints for objects.
- Object Instantiation: Instantiate objects from classes to utilize their unique state and identity.
- Instance vs. Class Attributes: Differentiate between attributes that belong to a class (shared) and those specific to an object (instance).
- Using self: Use the
selfkeyword in methods to refer to and manipulate the current instance. - Encapsulation: Bundle data and methods to keep object states intact and provide a clean interface for interaction.
Understanding these core concepts of object-oriented programming in Python empowers you to design more efficient, modular, and maintainable code. Practice encapsulating data and using classes to build the logical structure of your programs.
