CertGrid CertGrid
Concepts·Certified Associate Python Programmer

Python Instance and Class Variables

This is the most asked subject in PCAP's 34% object-oriented section, and it comes down to one rule: reading an attribute searches the instance and then the class; assigning one always writes to the instance. Everything surprising follows - the list shared between every instance, and the counter that increments to 1 three times over.

Object-Oriented Python Guide 15 of 25 Intermediate

Unchanged in Python 3. The attribute lookup order described here is the same mechanism that makes inheritance work - see guide 18.

One machine, and any shell with Python 3 will do - these exams test the language, not a distribution.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSPython 3.14.4 - the only machine this path needs2 Core4 GB50 GB

This guide includes

Use this because this is the most asked subject in PCAP's 34% object-oriented section. This matters because assigning through an instance creates an instance variable - so a mutable class variable is shared by every object, and a counter written the obvious way does not count.

Before you start

  1. Where each variable lives

  2. Assigning through an instance creates an instance variable

  3. The trap: a mutable class variable

  4. The fix

  5. What a class variable is actually for

  6. The counter that does not count

  7. The rule, in one line

  8. What the exam does with this objective

Official sources