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
- Class: A blueprint for creating objects. It defines a set of attributes and methods that the created objects will have.
- Object: An instance of a class. It is created using the class blueprint and can have its own unique attributes and methods.
- Attributes: Variables that belong to a class or an object.
- 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:
speciesis a class attribute shared by all instances of theDogclass. - Instance Attributes:
nameandageare instance attributes, unique to each instance of theDogclass. - Initializer Method:
__init__is a special method called when an instance of the class is created. It initializes the instance attributes. - Instance Methods:
descriptionandspeakare 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 WoofExplanation
- Creating Instances:
dog1anddog2are instances of theDogclass. - 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 wheelsCommon Mistakes and Tips
- Forgetting
self: Always includeselfas 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
- Introduction to Python
- Setting Up the Development Environment
- Python Syntax and Basic Data Types
- Variables and Constants
- Basic Input and Output
Module 2: Control Structures
Module 3: Functions and Modules
- Defining Functions
- Function Arguments
- Lambda Functions
- Modules and Packages
- Standard Library Overview
Module 4: Data Structures
Module 5: Object-Oriented Programming
Module 6: File Handling
Module 7: Error Handling and Exceptions
Module 8: Advanced Topics
- Decorators
- Generators
- Context Managers
- Concurrency: Threads and Processes
- Asyncio for Asynchronous Programming
Module 9: Testing and Debugging
- Introduction to Testing
- Unit Testing with unittest
- Test-Driven Development
- Debugging Techniques
- Using pdb for Debugging
Module 10: Web Development with Python
- Introduction to Web Development
- Flask Framework Basics
- Building REST APIs with Flask
- Introduction to Django
- Building Web Applications with Django
Module 11: Data Science with Python
- Introduction to Data Science
- NumPy for Numerical Computing
- Pandas for Data Manipulation
- Matplotlib for Data Visualization
- Introduction to Machine Learning with scikit-learn
