Courseiva
Object-Oriented ProgrammingmediumMultiple ChoiceObjective-mapped

Python Mutable Class Attribute vs Instance Attribute

An application uses a class to represent a configuration object that reads settings from a file. The class has a class attribute config_cache that holds a dictionary of loaded configurations to avoid repeated file reads. However, the developer notices that when they modify the dictionary for one instance, it affects all instances. They want to ensure that each instance has its own copy of the configuration data upon initialization. Which change should they make?

Quick Answer

The correct change is to move the dictionary initialization into the __init__ method so each instance creates its own dictionary. This resolves the issue because a mutable class attribute like config_cache is shared across all instances of the class; when you modify the dictionary through one instance, the change is reflected in every other instance since they all reference the same object in memory. By assigning self.config_cache = {} inside __init__, each new instance gets its own independent dictionary, preventing unintended shared state. On the Certified Associate Python Programmer PCAP exam, this question tests your understanding of the difference between mutable class attributes and instance attributes, a classic trap where beginners assume each instance gets its own copy of a mutable default. A reliable memory tip: class attributes are shared like a communal whiteboard, while instance attributes are personal notebooks—always initialize mutable objects like lists or dictionaries inside __init__ to avoid surprises.

⚠ Common exam trap

Python Institute often tests the distinction between mutable and immutable class attributes, trapping candidates who think a deep copy in __init__ will fix the sharing issue, when in fact the shared reference to the class attribute itself must be replaced with an instance attribute.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Move the dictionary initialization into the __init__ method so each instance creates its own dictionary.

Moving the dictionary initialization into the __init__ method ensures that each instance gets its own separate dictionary object. Class attributes are shared across all instances, so modifying the dictionary via one instance changes it for all. By assigning `self.config_cache = {}` inside __init__, each instance creates a new, independent dictionary upon instantiation, solving the shared-state problem.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Move the dictionary initialization into the __init__ method so each instance creates its own dictionary.

    Why this is correct

    Initializing in __init__ creates a new dictionary per instance, avoiding sharing.

  • Use a @staticmethod to return a new dictionary each time.

    Why it's wrong here

    A static method does not affect instance attributes.

  • Keep the class attribute but use a deep copy in __init__ before modifying.

    Why it's wrong here

    While a deep copy in `__init__` would create a distinct dictionary for each instance, it doesn't resolve the core problem of shared state originating from the class attribute `config_cache`. This option is tempting because deep copying is the standard technique for isolating mutable objects, and it would be correct if the goal was to prevent modifications to an instance's copy from affecting other instances *after* initialization, assuming each instance was intended to start with its own copy of some initial data.

  • Define the dictionary inside a class method.

    Why it's wrong here

    A class method still operates on class-level data.

About these practice questions

One of 169 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on PCAP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A class has a class attribute that is a list. A developer modifies this list via one instance, and the change is reflected in all other instances. What is the best practice to avoid this unintended sharing?

hard
  • A.Use a tuple instead of a list.
  • B.Initialize the list in `__init__` rather than as a class attribute.
  • C.Use a class method to modify the list.
  • D.Use `deepcopy` when accessing the list.

Why B: Class attributes are shared across all instances. By initializing the list inside `__init__`, each instance gets its own independent list object, preventing unintended mutation from affecting other instances. This is the standard Python pattern for instance-specific mutable data.

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.