OOP – Classes & Objects, online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
OOP – Classes & Objects in one minute
What students need to know
A class is a blueprint that defines the structure of a group of objects — what data they hold and what they can do. An object (also called an instance) is a specific thing created from that class, using the class's name like a function: Dog('Rex'). Inside a class's methods, self refers to the particular object the method is being called on, which is how each object keeps its own separate data.
Mistakes students make
classmust be written lowercase —Class Dog:is invalid Python, even though it looks like a small typo.- Forgetting
selfas the first parameter of a method is another common early mistake, since Python doesn't add it automatically.
PREVIOUS · MODULE 48Creating Modules (.py files to import)
YOU ARE HERE · MODULE 49OOP – Classes & Objects
NEXT · MODULE 50OOP – __init__ Method
What students will learn in this module
Every question in Module 49
Quick questions
- What is a class?AA blueprint for creating objectsBA function
- What is an object?AA class definitionBAn instance created from a class
- How do you create an object?Amy_obj = ClassName()Bdef ClassName():
- What does 'self' refer to?AThe current object instanceBThe class itself
- class Dog:\n pass\ndog = Dog()\nWhat is dog?AThe Dog classBAn instance of the Dog class
Key terms
Students type the meaning of:
ClassObjectInstanceself
Match the pairs
class dog:
passMatch it to what it doesclass Dog
passMatch it to what it doesclass Dog:
pass
d = Dog()Match it to what it doesclass Car:
pass
a = Car()
b = Car()Match it to what it doesclass Dog:
pass
d = Dog()
print(type(d))Match it to what it doesclass Cat:
pass
c = Cat()
c.name = 'Mia'
print(c.name)Match it to what it doesFill in the blanks
_____ Dog: passclass Dog: pass my_dog = ___()p = Person() p.____ = 'Alice'
class Car: ____print(____(my_dog))
Spot the error
Question 1
AClass Dog:B def __init__(self, name):C self.name = nameD def bark(self):E return 'Woof!'
Question 2
Aclass dog:B def __init__(self, name):C self.name = nameD def bark(self):E return 'Woof!'
Question 3
Aclass DogB def __init__(self, name):C self.name = nameD def bark(self):E return 'Woof!'
Question 4
Aclass Dog():B passCDmy_dog = Dog
Question 5
Adef Dog:B def __init__(self, name):C self.name = nameD def bark(self):E return 'Woof!'
