Classes vs Objects
Part of the Object Oriented Programming section of Coddy's Python journey — lesson 3 of 64.
Classes and objects serve different purposes. A class is a blueprint, while an object is what you build from that blueprint.
Here is an example of a class:
class Dog:
# shared by all dogs
species = "Canis familiaris"Now we can access the class attribute directly using Dog.species where Dog is the class name:
print(f"Class species: {Dog.species}")
# Class species: Canis familiarisThis accesses data from the class itself, not from any specific object.
Now lets explore how it works in objects. Create objects from the Dog class:
dog1 = Dog()
dog2 = Dog()Objects can have individual attributes that classes cannot:
dog1.name = "Nicky"
dog1.breed = "Siberian Husky"
dog2.name = "Teemon"
dog2.breed = "Shu'ali"Compare what classes and objects can do:
print(f"Object 1: {dog1.name} is a {dog1.breed}")
print(f"Object 2: {dog2.name} is a {dog2.breed}")
print(f"Both share class attribute: {Dog.species}")Output:
Object 1: Nicky is a Siberian Husky
Object 2: Teemon is a Shu'ali
Both share class attribute: Canis familiarisKey Difference: The class Dog defines what all dogs have in common, while objects dog1 and dog2 represent specific, individual dogs with unique properties.
Challenge
EasyComplete the code to create a Student class and two student objects. Add different names and grades to each student.
student.py: This is where you'll define yourStudentclass.driver.py: Main execution file that will import and use your Student class:Complete the code to create a
Studentclass and two student objects. Add different names and grades to each student.student1 name is
"Alice"with grade"A".student2 name is
"Bob"with grade"B".
Cheat sheet
A class is a blueprint that defines shared attributes and behaviors. An object is an instance created from that class with individual properties.
Define a class:
class Dog:
# Class attribute - shared by all instances
species = "Canis familiaris"Access class attributes directly:
print(Dog.species) # Canis familiarisCreate objects from a class:
dog1 = Dog()
dog2 = Dog()Add individual attributes to objects:
dog1.name = "Nicky"
dog1.breed = "Siberian Husky"
dog2.name = "Teemon"
dog2.breed = "Shu'ali"Objects can access both their individual attributes and class attributes:
print(f"{dog1.name} is a {dog1.breed}") # Individual attributes
print(f"Species: {Dog.species}") # Class attributeTry it yourself
# Import the Student class
from student import Student
# TODO: Create two student objects
# TODO: Add attributes to student1
# TODO: Add attributes to student2
# Print student information
print(f"{student1.name} has grade {student1.grade} at {Student.school}")
print(f"{student2.name} has grade {student2.grade} at {Student.school}")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