Class attributes store information shared by all objects of a class. They help keep common data in one place.
Class attributes in Python
Start learning this pattern below
Jump into concepts and practice - no test required
class ClassName:
class_attribute = valueClass attributes are defined directly inside the class, but outside any methods.
All instances share the same class attribute unless overridden in the instance.
species is a class attribute shared by all Dog objects.class Dog: species = "Canis familiaris"
car1 and car2 share the same wheels class attribute.class Car: wheels = 4 car1 = Car() car2 = Car() print(car1.wheels) # prints 4 print(car2.wheels) # prints 4
total_books counts how many Book objects are created.class Book: total_books = 0 def __init__(self): Book.total_books += 1
This program shows how the class attribute school_name is shared by all Student objects. Changing it on the class changes it for all students.
class Student: school_name = "Greenwood High" def __init__(self, name): self.name = name # Create two students student1 = Student("Alice") student2 = Student("Bob") # Print their school name (class attribute) print(student1.name + " goes to " + student1.school_name) print(student2.name + " goes to " + student2.school_name) # Change class attribute Student.school_name = "Sunrise Academy" print(student1.name + " now goes to " + student1.school_name) print(student2.name + " now goes to " + student2.school_name)
If you assign a value to a class attribute using an instance (like student1.school_name = 'New School'), it creates a new instance attribute and does not change the class attribute.
Use class attributes for data shared by all instances, and instance attributes for data unique to each object.
Class attributes hold data shared by all objects of a class.
They are defined inside the class but outside methods.
Changing a class attribute affects all instances unless they have their own attribute with the same name.
Practice
class Car: wheels = 4Here, what does
wheels represent?Solution
Step 1: Identify the attribute location
The attributewheelsis 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 AQuick Check:
Class attribute = shared value [OK]
- Thinking class attributes are unique per object
- Confusing class attributes with instance attributes
- Assuming class attributes are methods
color with value "red" inside a class Fruit?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 definescoloras 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 DQuick Check:
Class attribute = direct assignment inside class [OK]
- Defining attribute inside __init__ without self
- Confusing methods with attributes
- Using self for class attributes
class Dog:
species = "Canine"
dog1 = Dog()
dog2 = Dog()
Dog.species = "Wolf"
print(dog1.species)
print(dog2.species)Solution
Step 1: Understand class attribute change
The attributespeciesis a class attribute shared by all instances.Step 2: Effect of changing class attribute
ChangingDog.speciesto "Wolf" updates the value for all instances that don't have their ownspeciesattribute.Final Answer:
Wolf\nWolf -> Option CQuick Check:
Changing class attribute affects all instances [OK]
- Thinking instances keep old class attribute values
- Confusing instance and class attributes
- Expecting different outputs for dog1 and dog2
class Book:
pages = 100
def __init__(self, pages):
pages = pages
b = Book(200)
print(b.pages)Solution
Step 1: Analyze __init__ method
The linepages = pagesassigns the parameter to itself, not to the instance.Step 2: Understand instance attribute setting
To set an instance attribute, it should beself.pages = pages. Withoutself., the instance attribute is not created.Final Answer:
The instance attribute pages is not set properly -> Option AQuick Check:
Use self.pages to set instance attribute [OK]
- Forgetting self. when assigning instance attributes
- Assuming parameter assignment sets instance attribute
- Confusing class and instance attributes
Student are created using a class attribute count. Which code correctly updates count each time a new Student is made?Solution
Step 1: Identify class attribute usage
countis 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 usesStudent.count += 1, correctly updating the class attribute. class Student: count = 0 def __init__(self): self.count += 1 tries to incrementself.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 BQuick Check:
Update class attribute via ClassName.attribute [OK]
- Using self.attribute to update class attribute
- Defining count inside __init__ instead of class
- Forgetting to use class name to access class attribute
