Courseiva
Object-Oriented ProgramminghardMultiple ChoiceObjective-mapped

Understanding Class Attributes Shared Across All Instances

A class `ServerConfig` has a class attribute `port = 8080`. After deployment, a developer runs `ServerConfig.port = 9090` in one module, and unexpectedly all existing instances now use port 9090. What concept explains this behavior?

Quick Answer

The correct answer is that class attributes are shared across all instances, which is why modifying `ServerConfig.port` on the class itself immediately changes the value seen by every existing instance. This happens because Python’s attribute lookup first checks the instance namespace, and if no instance attribute exists, it falls back to the class attribute—so when you set `ServerConfig.port = 9090`, you are updating the single shared value in the class namespace, not creating an instance-specific copy. On the PCAP exam, this concept tests your understanding of Python’s attribute resolution order and the distinction between class and instance attributes; a common trap is assuming each instance gets its own copy of a class attribute, or that reassigning via the class only affects new instances. To remember: think of class attributes as a shared whiteboard—write on the board itself, and everyone reads the new message; write on an instance’s sticky note, and only that instance sees it.

⚠ Common exam trap

Python Institute often tests the distinction between modifying a class attribute via the class vs. modifying it via an instance; the trap is that candidates think assigning to `instance.port` changes the class attribute, but it actually creates a new instance attribute that shadows the class 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

Class attributes are shared among all instances of a class.

Class attributes in Python are shared across all instances of a class. When you modify `ServerConfig.port` on the class itself, every instance that accesses `port` via the class (or via an instance that hasn't overridden it) sees the new value. This is fundamental to Python's attribute lookup mechanism: instance attributes shadow class attributes, but if no instance attribute exists, the class attribute is used.

Answer analysis

Option-by-option breakdown

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

  • Instance attributes always override class attributes.

    Why it's wrong here

    This explains why modifying via an instance would not affect the class, but the scenario modified via the class.

  • The attribute is immutable.

    Why it's wrong here

    Immutability would prevent modification altogether, not explain the propagation.

  • Class attributes are shared among all instances of a class.

    Why this is correct

    Changing a class attribute via the class affects all instances, as they all reference the same attribute.

  • Python uses copy-on-write for attribute access.

    Why it's wrong here

    Copy-on-write is not a Python mechanism for attribute access.

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

2 more ways 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. Which TWO of the following statements about class attributes in Python are true?

easy
  • A.Class attributes are always immutable.
  • B.Class attributes are shared by all instances.
  • C.Class attributes are defined inside methods.
  • D.Modifying a class attribute via an instance modifies it for all instances.
  • E.Class attributes can be accessed via the class name.

Why B: Class attributes are defined directly in the class body and are shared across all instances of that class. When you access a class attribute via any instance, Python looks up the attribute in the class's __dict__ if it is not shadowed by an instance attribute, ensuring all instances see the same value unless explicitly overridden.

Variation 2. Which TWO of the following statements about Python classes are true? (Select exactly 2.)

medium
  • A.Class names should be written in snake_case per PEP 8.
  • B.Class variables are shared among all instances.
  • C.Private attributes (starting with __) cannot be accessed outside the class.
  • D.__init__ is the constructor of a class.
  • E.Instance methods must have 'self' as the first parameter.

Why B: Class variables are defined directly in the class body and are shared across all instances of that class. When you modify a class variable through the class itself, the change is reflected in every instance, as the variable is stored in the class's __dict__ rather than in each instance's __dict__.

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.