Recall & Review
beginner
What is an object in Python?
An object is a collection of data (attributes) and functions (methods) that act on the data. It represents a real-world thing or concept.
Click to reveal answer
beginner
How do you create an object from a class in Python?You create an object by calling the class name followed by parentheses, like <code>obj = ClassName()</code>.Click to reveal answer
beginner
What is the role of the
__init__ method in a class?The
__init__ method initializes a new object's attributes when it is created. It runs automatically.Click to reveal answer
beginner
What does this code do?<br><pre>class Dog:
def __init__(self, name):
self.name = name
my_dog = Dog('Buddy')</pre>It defines a class Dog with an initializer that sets the dog's name. Then it creates an object my_dog with the name 'Buddy'.Click to reveal answer
beginner
Why do we use <code>self</code> in class methods?self refers to the current object. It lets methods access or change the object's attributes.Click to reveal answer
How do you create an object from a class named
Car?✗ Incorrect
You create an object by calling the class name with parentheses:
my_car = Car().What is the purpose of the
__init__ method?✗ Incorrect
The
__init__ method sets up the object's attributes when it is created.In the method definition
def greet(self):, what does self mean?✗ Incorrect
self refers to the current object inside class methods.Which of these is a correct way to define a class in Python?
✗ Incorrect
The correct syntax is
class Animal: followed by an indented block.What happens when you run
obj = MyClass()?✗ Incorrect
Calling
MyClass() creates a new object of that class.Explain how to create an object from a class in Python and why the
__init__ method is important.Think about how you make a new thing from a blueprint and set its details.
You got /3 concepts.
Describe the role of
self in class methods and why it is necessary.Imagine you talking about yourself when describing your own features.
You got /3 concepts.