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 is a class attribute in Python?
A class attribute is a variable that is shared by all instances of a class. It is defined inside the class but outside any instance methods.
Click to reveal answer
beginner
How do class attributes differ from instance attributes?
Class attributes are shared by all instances of the class, while instance attributes belong to each individual object and can have different values.
Click to reveal answer
beginner
Given this code:<br><pre>class Dog:
species = 'Canis familiaris'
def __init__(self, name):
self.name = name</pre><br>What is <code>species</code> here?
<code>species</code> is a class attribute. It is shared by all Dog objects and holds the value 'Canis familiaris'.
Click to reveal answer
intermediate
Can you change a class attribute value for all instances? How?
Yes, by assigning a new value to the class attribute using the class name, e.g., <code>ClassName.attribute = new_value</code>. This changes it for all instances that don't override it.
Click to reveal answer
intermediate
What happens if you assign a value to a class attribute name using an instance?
Assigning a value to a class attribute name via an instance creates or changes an instance attribute with the same name, which hides the class attribute for that instance only.
Click to reveal answer
Where are class attributes defined in Python?
AInside the class but outside any methods
BInside instance methods only
COutside the class definition
DInside the __init__ method only
✗ Incorrect
Class attributes are defined inside the class but outside any instance methods.
What is shared by all instances of a class?
AInstance attributes
BLocal variables
CClass attributes
DFunction parameters
✗ Incorrect
Class attributes are shared by all instances, while instance attributes are unique to each object.
If you change a class attribute using the class name, what happens?
AAll instances see the updated value unless they have an instance attribute with the same name
BOnly the first instance changes
CNothing changes for any instance
DOnly new instances get the change
✗ Incorrect
Changing a class attribute via the class name updates it for all instances that do not override it.
What happens if you assign a value to a class attribute name using an instance?
AThe class attribute changes for all instances
BAn instance attribute is created or updated, hiding the class attribute for that instance
CAn error occurs
DThe class attribute is deleted
✗ Incorrect
Assigning to a class attribute name via an instance creates or updates an instance attribute, hiding the class attribute.
Which of these is a correct way to access a class attribute?
AClassName.__init__()
Binstance.attribute()
Cinstance.__init__()
DClassName.attribute
✗ Incorrect
Class attributes are accessed using the class name followed by the attribute name.
Explain what a class attribute is and how it differs from an instance attribute.
Think about a property all objects share versus something unique to each.
You got /4 concepts.
Describe what happens when you assign a value to a class attribute name using an instance.
Consider how Python handles attribute lookup and shadowing.
You got /3 concepts.
Practice
(1/5)
1. What is a class attribute in Python? class Car: wheels = 4 Here, what does wheels represent?
easy
A. A value shared by all Car objects
B. A value unique to each Car object
C. A method inside the Car class
D. A variable defined inside a method
Solution
Step 1: Identify the attribute location
The attribute wheels is defined inside the class but outside any method.
Step 2: Understand class attribute behavior
Attributes defined this way are shared by all instances of the class.
Final Answer:
A value shared by all Car objects -> Option A
Quick Check:
Class attribute = shared value [OK]
Hint: Class attributes are outside methods, shared by all instances [OK]
Common Mistakes:
Thinking class attributes are unique per object
Confusing class attributes with instance attributes
Assuming class attributes are methods
2. Which of the following is the correct way to define a class attribute color with value "red" inside a class Fruit?
easy
A. class Fruit:
def __init__(self):
color = "red"
B. class Fruit:
def color(self):
return "red"
C. class Fruit:
def __init__(self, color):
self.color = color
D. class Fruit:
color = "red"
Solution
Step 1: Identify class attribute syntax
A class attribute is defined directly inside the class, outside any method.
Step 2: Check each option
class Fruit:
color = "red" correctly defines color as a class attribute. class Fruit:
def __init__(self):
color = "red" defines a local variable inside __init__. class Fruit:
def color(self):
return "red" defines a method, not an attribute. class Fruit:
def __init__(self, color):
self.color = color defines an instance attribute.
Final Answer:
class Fruit:
color = "red" -> Option D
Quick Check:
Class attribute = direct assignment inside class [OK]
Hint: Class attributes are assigned directly inside class, outside methods [OK]
Common Mistakes:
Defining attribute inside __init__ without self
Confusing methods with attributes
Using self for class attributes
3. What will be the output of this code?
class Dog:
species = "Canine"
dog1 = Dog()
dog2 = Dog()
Dog.species = "Wolf"
print(dog1.species)
print(dog2.species)
medium
A. Canine\nCanine
B. Wolf\nCanine
C. Wolf\nWolf
D. Canine\nWolf
Solution
Step 1: Understand class attribute change
The attribute species is a class attribute shared by all instances.
Step 2: Effect of changing class attribute
Changing Dog.species to "Wolf" updates the value for all instances that don't have their own species attribute.
Final Answer:
Wolf\nWolf -> Option C
Quick Check:
Changing class attribute affects all instances [OK]
Hint: Changing class attribute changes it for all instances without override [OK]
Common Mistakes:
Thinking instances keep old class attribute values
Confusing instance and class attributes
Expecting different outputs for dog1 and dog2
4. Find the error in this code snippet:
class Book:
pages = 100
def __init__(self, pages):
pages = pages
b = Book(200)
print(b.pages)
medium
A. The instance attribute pages is not set properly
B. The class attribute pages is overwritten incorrectly
C. Syntax error in __init__ method
D. Cannot print pages attribute directly
Solution
Step 1: Analyze __init__ method
The line pages = pages assigns the parameter to itself, not to the instance.
Step 2: Understand instance attribute setting
To set an instance attribute, it should be self.pages = pages. Without self., the instance attribute is not created.
Final Answer:
The instance attribute pages is not set properly -> Option A
Quick Check:
Use self.pages to set instance attribute [OK]
Hint: Use self.attribute to set instance attributes inside __init__ [OK]
Common Mistakes:
Forgetting self. when assigning instance attributes
5. You want to keep track of how many objects of class Student are created using a class attribute count. Which code correctly updates count each time a new Student is made?
hard
A. class Student:
count = 0
def __init__(self):
self.count += 1
B. class Student:
count = 0
def __init__(self):
Student.count += 1
C. class Student:
def __init__(self):
count = 0
count += 1
D. class Student:
count = 0
def __init__(self):
count += 1
Solution
Step 1: Identify class attribute usage
count is a class attribute, so it should be accessed via the class name inside methods.
Step 2: Check each option for correct increment
class Student:
count = 0
def __init__(self):
Student.count += 1 uses Student.count += 1, correctly updating the class attribute. class Student:
count = 0
def __init__(self):
self.count += 1 tries to increment self.count, which creates an instance attribute instead. Options C and D have syntax or scope errors.
Final Answer:
class Student:
count = 0
def __init__(self):
Student.count += 1 -> Option B
Quick Check:
Update class attribute via ClassName.attribute [OK]
Hint: Use ClassName.attribute to update class attributes inside methods [OK]
Common Mistakes:
Using self.attribute to update class attribute
Defining count inside __init__ instead of class
Forgetting to use class name to access class attribute