Introduction to OOP
Part of the Object Oriented Programming section of Coddy's Python journey — lesson 2 of 64.
Object-Oriented Programming (OOP) organizes code around objects that contain data (attributes) and functions (methods).
Create a file called car.py with a class
class Car:
pass # placeholder that does nothingCreate another file called driver.py to use the class
from car import CarCreate an object from the Car class
my_car = Car()Check the type of your object
print(type(my_car))Output:
<class 'car.Car'>This confirms you've successfully created an object from your Car class. In OOP, a class is like a blueprint, and an object is what you build from that blueprint.
Challenge
MediumIn this challenge, you'll implement a Car class in car.py and use it in driver.py.
You need to update driver.py to import and use the Car class properly
Detailed TODO comments will guide you through each step of the implementation. The driver file contains test cases that will verify your implementation works correctly.
Cheat sheet
Object-Oriented Programming (OOP) organizes code around objects that contain data (attributes) and functions (methods).
Create a class using the class keyword:
class Car:
pass # placeholder that does nothingImport a class from another file:
from car import CarCreate an object from a class:
my_car = Car()Check the type of an object:
print(type(my_car))
# Output: <class 'car.Car'>In OOP, a class is like a blueprint, and an object is what you build from that blueprint.
Try it yourself
from car import Car
# TODO: Create an object from the class
my_car = ?
# Print the type of my_car
print(type(my_car))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe self ParameterMethodsAttributesConstructor Method (__init__)Recap - Simple Calculator4Inheritance
Basic InheritanceThe super() FunctionMethod OverridingMultiple InheritanceMethod Resolution OrderRecap - Employee Hierarchy7Special Methods
Magic Methods IntroductionOperator OverloadingContainer Magic MethodsRecap - Custom List10Design Patterns Part 1
Intro to design patternSingleton PatternFactory PatternObserver PatternStrategy Pattern2Decorators
Introduction to DecoratorsProperty DecoratorStatic Method DecoratorClass Method Decorator5Polymorphism
Method Overriding RevisitedDuck TypingAbstract Classes and MethodsInterface DesignRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceMixinsStatic and Class MethodsClass DecoratorsContext Managers3Class Properties
Instance vs Class VariablesProperty DecoratorsPrivate AttributesRecap - Bank Account Manager6Encapsulation
Public, Protected, Private MemAccess ModifiersInformation HidingProperty Decorators AdvancedRecap - Student Records System12Project: Library Management
Project OverviewBook and User Classes