Introduction

In Python, Object-Oriented Programming (OOP) is a paradigm that uses "objects" to model real-world entities. This module will introduce you to the fundamental concepts of OOP, focusing on classes and objects.

Key Concepts

  1. Class: A blueprint for creating objects. It defines a set of attributes and methods that the created objects will have.
  2. Object: An instance of a class. It is created using the class blueprint and can have its own unique attributes and methods.
  3. Attributes: Variables that belong to a class or an object.
  4. Methods: Functions that belong to a class and can be called on objects of that class.

Defining a Class

A class is defined using the class keyword followed by the class name and a colon. Inside the class, you can define attributes and methods.

class Dog:
    # Class attribute
    species = "Canis familiaris"

    # Initializer / Instance attributes
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # Instance method
    def description(self):
        return f"{self.name} is {self.age} years old"

    # Another instance method
    def speak(self, sound):
        return f"{self.name} says {sound}"

Explanation

  • Class Attribute: species is a class attribute shared by all instances of the Dog class.
  • Instance Attributes: name and age are instance attributes, unique to each instance of the Dog class.
  • Initializer Method: __init__ is a special method called when an instance of the class is created. It initializes the instance attributes.
  • Instance Methods: description and speak are methods that operate on the instance attributes.

Creating Objects

Objects are instances of a class. You create an object by calling the class as if it were a function.

# Creating instances of the Dog class
dog1 = Dog("Buddy", 3)
dog2 = Dog("Lucy", 5)

# Accessing class attributes
print(dog1.species)  # Output: Canis familiaris

# Accessing instance attributes
print(dog1.name)  # Output: Buddy
print(dog2.age)   # Output: 5

# Calling instance methods
print(dog1.description())  # Output: Buddy is 3 years old
print(dog2.speak("Woof Woof"))  # Output: Lucy says Woof Woof

Explanation

  • Creating Instances: dog1 and dog2 are instances of the Dog class.
  • Accessing Attributes: You can access class and instance attributes using dot notation.
  • Calling Methods: You can call instance methods using dot notation.

Practical Exercise

Exercise 1: Define a Class

Define a class Car with the following attributes and methods:

  • Class Attribute: wheels (default value: 4)
  • Instance Attributes: make, model, year
  • Method: description (returns a string describing the car)
class Car:
    wheels = 4

    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

    def description(self):
        return f"{self.year} {self.make} {self.model} with {self.wheels} wheels"

Exercise 2: Create Objects

Create two instances of the Car class and print their descriptions.

# Creating instances of the Car class
car1 = Car("Toyota", "Corolla", 2020)
car2 = Car("Honda", "Civic", 2018)

# Printing descriptions
print(car1.description())  # Output: 2020 Toyota Corolla with 4 wheels
print(car2.description())  # Output: 2018 Honda Civic with 4 wheels

Common Mistakes and Tips

  • Forgetting self: Always include self as the first parameter in instance methods.
  • Class vs. Instance Attributes: Remember that class attributes are shared among all instances, while instance attributes are unique to each instance.
  • Initialization: Use the __init__ method to initialize instance attributes.

Conclusion

In this section, you learned about the basics of classes and objects in Python. You now know how to define a class, create objects, and use attributes and methods. This foundational knowledge will be crucial as you move on to more advanced OOP concepts like inheritance, polymorphism, and encapsulation.

Python Programming Course

Module 1: Introduction to Python

Module 2: Control Structures

Module 3: Functions and Modules

Module 4: Data Structures

Module 5: Object-Oriented Programming

Module 6: File Handling

Module 7: Error Handling and Exceptions

Module 8: Advanced Topics

Module 9: Testing and Debugging

Module 10: Web Development with Python

Module 11: Data Science with Python

Module 12: Final Project

© Copyright 2024. All rights reserved