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
Class Attributes in Python
📖 Scenario: Imagine you are creating a simple program to keep track of cars in a parking lot. Each car has its own color and model, but all cars share the same parking lot name.
🎯 Goal: You will create a class called Car with a class attribute for the parking lot name. Then, you will create individual cars with their own colors and models, and finally print the parking lot name.
📋 What You'll Learn
Create a class called Car
Add a class attribute called parking_lot with the value 'Central Parking'
Create two instances of Car with different color and model attributes
Print the class attribute parking_lot from one of the instances
💡 Why This Matters
🌍 Real World
Class attributes are useful when you want to store information that is the same for all objects of a class, like a company name or a shared setting.
💼 Career
Understanding class attributes is important for writing clean and efficient object-oriented code, which is common in software development jobs.
Progress0 / 4 steps
1
Create the Car class with no attributes
Write a class called Car with no attributes or methods yet.
Python
Hint
Use the class keyword followed by the class name Car and a colon.
2
Add a class attribute parking_lot
Inside the Car class, add a class attribute called parking_lot and set it to the string 'Central Parking'.
Python
Hint
Class attributes are defined directly inside the class, but outside any methods.
3
Create two Car instances with color and model
Create two variables called car1 and car2 that are instances of the Car class. Then add instance attributes color and model to each car with these exact values: car1.color = 'red', car1.model = 'sedan', car2.color = 'blue', car2.model = 'suv'.
Python
Hint
Create instances by calling the class name with parentheses, then add attributes using dot notation.
4
Print the class attribute parking_lot from car1
Write a print statement to display the value of the class attribute parking_lot using the instance car1.
Python
Hint
Use print(car1.parking_lot) to access the class attribute from the instance.
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