Day 13 of 100 Days of Python Learning
Hello, fellow learners! 🎉 Welcome to Day 13 of our exciting 100-day Python learning journey. Today, we're diving into the fascinating world of Python Classes and Objects, the building blocks of Object-Oriented Programming (OOP). Whether you're a beginner or an intermediate coder, get ready to unlock the magic of classes and objects through a series of easy-to-difficult examples.
Python Classes and Objects: The Blueprint for Magic
Imagine having a recipe that not only lists ingredients but also tells you how to mix and bake them to create a delicious dish. That's exactly what a class does in Python. It's your blueprint for creating objects, packed with attributes and methods that define their behavior.
The Basics: Creating a Class
Let's start with a simple example. Imagine you're designing a class to represent a Person:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
person1 = Person("Alice", 30)
print(f"{person1.name} is {person1.age} years old.")In this snippet, you create a class Person with attributes name and age. You then create an object person1 based on that class and access its attributes.
Adding Methods to a Class
But classes can do more than just hold data. They can have methods too. Let's add a method to our Person class to introduce themselves:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"Hi, I'm {self.name} and I'm {self.age} years old.")
person1 = Person("Alice", 30)
person1.introduce()Now, the Person objects can introduce themselves!
Class Inheritance: Taking it Further
One of the coolest aspects of OOP is inheritance. Imagine you have a class Student that inherits from Person:
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
def display_student_info(self):
print(f"Student {self.name} (ID: {self.student_id}) is {self.age} years old.")
student1 = Student("Bob", 21, "12345")
student1.display_student_info()Here, the Student class inherits attributes and methods from the Person class and adds its own unique attributes and methods.
Magic Methods: The Hidden Power
Python classes have special methods, often called magic methods, that provide built-in functionality. Let's explore the magic of __str__ to create a custom string representation:
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
def __str__(self):
return f"{self.title} by {self.author}"
book1 = Book("Python 101", "John Doe")
print(book1)The __str__ magic method allows you to define how an object should be represented as a string.
Advanced Usage: Object Comparisons
You can also define custom behavior for comparisons using magic methods like __eq__ for equality:
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def __eq__(self, other):
return self.width == other.width and self.height == other.height
rect1 = Rectangle(3, 4)
rect2 = Rectangle(3, 4)
print(rect1 == rect2) # Outputs: TrueThis snippet demonstrates how to customize object comparisons using __eq__.
As you delve into the world of Python Classes and Objects, you're gaining a powerful tool for structured and organized programming.
Stay curious, keep coding, and let's embrace the magic of OOP together.
#PythonLearning #100DaysOfCode #OOPMagic #CodingJourney