> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Python Class Attribute

A class attribute is a variable bound to a class object rather than to a specific instance of that class. It is defined within the class body, outside of any instance methods (such as `__init__`), and its memory allocation is shared across all instances instantiated from that class.

Because class attributes are defined within the lexical scope of the class body, they reside in the class's namespace (`ClassName.__dict__`) rather than the instance's namespace (`instance.__dict__`).

```python theme={"dark"}
class ExampleClass:
    # This is a class attribute
    shared_state = "initial_value" 

    def __init__(self, value):
        # This is an instance attribute
        self.instance_state = value 
```

## Attribute Resolution Order

When you access an attribute via an instance (`obj.attribute`), Python invokes `__getattribute__` and follows a strict resolution order:

1. It first checks the class hierarchy, traversing the Method Resolution Order (`__mro__`), for a **data descriptor** (an object defining both `__get__` and `__set__` methods, such as a `@property`).
2. If no data descriptor is found, it checks the instance's namespace (`obj.__dict__`).
3. If the attribute is not found in the instance namespace, it searches the class hierarchy sequentially according to the MRO (where the immediate class is simply the first element in this sequence), looking for a non-data descriptor or a regular class attribute.

This mechanism allows instances to read class attributes as if they were their own, provided they are not shadowed by an instance attribute or intercepted by a data descriptor.

```python theme={"dark"}
obj1 = ExampleClass("A")
obj2 = ExampleClass("B")


# Accessing via the class
print(ExampleClass.shared_state)  # Output: initial_value


# Accessing via the instance (resolved via MRO fallback)
print(obj1.shared_state)          # Output: initial_value
```

## Modification and Shadowing

The behavior of modifying a class attribute depends strictly on whether the modification is performed via the class object or the instance object, and whether the assignment operation is a reassignment or a mutation.

**1. Modifying via the Class**
Assigning a new value directly to the class attribute updates the value in the class namespace. This change is immediately reflected across all instances that do not have a shadowing instance attribute.

```python theme={"dark"}
ExampleClass.shared_state = "modified_by_class"
print(obj1.shared_state)  # Output: modified_by_class
print(obj2.shared_state)  # Output: modified_by_class
```

**2. Reassigning via an Instance (Shadowing)**
Assigning a value to a class attribute via an instance does *not* modify the class attribute. Instead, it creates a new instance attribute with the same name in that specific instance's `__dict__`. This new instance attribute "shadows" the class attribute for that specific object during lookup (Step 2 of the resolution order).

```python theme={"dark"}

# Creates an instance attribute 'shared_state' in obj1
obj1.shared_state = "shadowed_by_instance"

print(obj1.shared_state)          # Output: shadowed_by_instance (reads from obj1.__dict__)
print(obj2.shared_state)          # Output: modified_by_class (reads via MRO)
print(ExampleClass.shared_state)  # Output: modified_by_class
```

**3. Mutating a Mutable Class Attribute via an Instance**
If a class attribute is a mutable object (like a `list` or `dict`), calling a mutating method (e.g., `.append()`, `.update()`) via an instance modifies the shared object in place. Because no assignment (`=`) occurs to the attribute name itself, Python does not create a shadowing instance attribute.

```python theme={"dark"}
class MutableExample:
    shared_list = []

m1 = MutableExample()
m2 = MutableExample()


# Mutating the shared object in place
m1.shared_list.append("new_item")

print(m2.shared_list)             # Output: ['new_item']
print(MutableExample.shared_list) # Output: ['new_item']
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
