Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What are instance attributes in Python?
Instance attributes are variables that belong to a specific object created from a class. Each object can have different values for these attributes.
Click to reveal answer
beginner
How do you define an instance attribute inside a class?
You define instance attributes inside the __init__ method using self, like: self.attribute_name = value.
Click to reveal answer
intermediate
What is the difference between instance attributes and class attributes?
Instance attributes belong to each object separately, while class attributes are shared by all objects of the class.
Click to reveal answer
intermediate
Can instance attributes be added outside the __init__ method?
Yes, you can add or change instance attributes anytime using dot notation, like object.attribute = value.
Click to reveal answer
beginner
What happens if you try to access an instance attribute that does not exist?
Python raises an AttributeError because the object does not have that attribute.
Click to reveal answer
Where are instance attributes usually defined in a Python class?
AInside a static method
BOutside the class
CAs global variables
DInside the __init__ method using self
✗ Incorrect
Instance attributes are typically set inside the __init__ method using self to attach them to the object.
What keyword is used to refer to the current object inside a class?
Aself
Bthis
Cme
Dobj
✗ Incorrect
In Python, 'self' refers to the current object inside class methods.
If you change an instance attribute on one object, does it affect other objects of the same class?
AOnly if the attribute is a class attribute
BYes, all objects change
CNo, only that object changes
DOnly if the attribute is private
✗ Incorrect
Instance attributes belong to each object separately, so changing one does not affect others.
What error occurs if you try to access a missing instance attribute?
AAttributeError
BNameError
CTypeError
DValueError
✗ Incorrect
Accessing a non-existent attribute raises an AttributeError.
Can you add new instance attributes after an object is created?
ANo, attributes must be defined in __init__
BYes, using dot notation on the object
COnly if the class allows it explicitly
DOnly for class attributes
✗ Incorrect
Python allows adding new instance attributes anytime using dot notation.
Explain what instance attributes are and how they differ from class attributes.
Think about how each object can have its own data.
You got /3 concepts.
Describe how to create and access instance attributes in a Python class.
Remember the role of self inside methods.
You got /3 concepts.
Practice
(1/5)
1. What is an instance attribute in Python classes?
easy
A. A variable shared by all objects of the class
B. A function that belongs to the class
C. A method to create new objects
D. A variable that stores data unique to each object
Solution
Step 1: Understand instance attributes
Instance attributes are variables that belong to each object separately, not shared.
Step 2: Differentiate from class attributes
Class attributes are shared by all objects, but instance attributes hold unique data per object.
Final Answer:
A variable that stores data unique to each object -> Option D
Quick Check:
Instance attribute = unique data per object [OK]
Hint: Instance attributes belong to objects, not the class itself [OK]
Common Mistakes:
Confusing instance attributes with class attributes
Thinking methods are attributes
Assuming all objects share the same attribute values
2. Which of the following is the correct way to define an instance attribute inside a class?
easy
A. name = "Alice" outside any method
B. def name(self): return "Alice"
C. self.name = "Alice" inside __init__ method
D. class.name = "Alice" inside __init__
Solution
Step 1: Identify instance attribute syntax
Instance attributes are set inside __init__ using self.attribute = value.
Step 2: Check each option
self.name = "Alice" inside __init__ method uses self.name = "Alice" inside __init__, which is correct. Others are class attributes, methods, or invalid.
Final Answer:
self.name = "Alice" inside __init__ method -> Option C
Hint: Use self.attribute = value inside __init__ for instance attributes [OK]
Common Mistakes:
Defining attributes outside __init__ without self
Using class.attribute instead of self.attribute
Confusing methods with attributes
3. What will be the output of this code?
class Dog:
def __init__(self, name):
self.name = name
dog1 = Dog("Buddy")
dog2 = Dog("Max")
print(dog1.name)
print(dog2.name)
medium
A. Buddy
Max
B. Max
Buddy
C. Buddy
Buddy
D. Max
Max
Solution
Step 1: Understand instance attribute assignment
dog1.name is set to "Buddy" and dog2.name is set to "Max" separately.
Step 2: Print instance attributes
Printing dog1.name outputs "Buddy" and dog2.name outputs "Max".
Final Answer:
Buddy
Max -> Option A
Quick Check:
Each object has its own name attribute [OK]
Hint: Each object keeps its own attribute values [OK]
Common Mistakes:
Assuming all objects share the same attribute
Mixing up the order of print statements
Confusing class and instance attributes
4. Find the error in this code:
class Car:
def __init__(self, model):
model = model
car = Car("Tesla")
print(car.model)
medium
A. AttributeError because model is not set as instance attribute
B. SyntaxError due to missing self
C. TypeError because __init__ has wrong parameters
D. No error, prints Tesla
Solution
Step 1: Check attribute assignment in __init__
The code assigns model = model, which only assigns local variable, not instance attribute.
Step 2: Accessing car.model causes error
Since self.model is never set, car.model does not exist, causing AttributeError.
Final Answer:
AttributeError because model is not set as instance attribute -> Option A
Quick Check:
Use self.model = model to set instance attribute [OK]
Hint: Always use self.attribute = value to set instance attributes [OK]
Common Mistakes:
Forgetting self. when assigning attributes
Assuming local variable sets instance attribute
Expecting attribute to exist without self
5. You want to create a class Book where each book has a title and a list of authors. How do you correctly set instance attributes so each book has its own authors list without sharing it between objects?
hard
A. Set self.authors = None and assign list later
B. Set self.authors = [] inside __init__ method
C. Set self.authors = authors where authors is a default empty list in parameters
D. Set authors = [] as a class attribute outside methods
Solution
Step 1: Avoid shared mutable class attributes
Setting authors = [] as class attribute shares the same list across all objects, causing bugs.