> ## 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 Instance Attribute

An instance attribute is a variable bound to a specific, instantiated object of a class rather than the class itself. Each object maintains its own independent copy of the attribute, meaning state modifications to an instance attribute on one object do not affect other objects of the same class.

Instance attributes are typically defined and initialized within the class constructor (the `__init__` method) using the `self` reference, which represents the current object instance in memory.

```python theme={"dark"}
class Connection:
    def __init__(self, host, port):
        self.host = host  # Instance attribute
        self.port = port  # Instance attribute
```

## Underlying Mechanism

Python stores instance attributes in a dedicated namespace dictionary for each object, accessible via the `__dict__` magic attribute. When an attribute is accessed via dot notation (`object.attribute`), Python resolves it by first checking the instance's `__dict__`. If the attribute is not found there, the interpreter falls back to checking the class `__dict__`.

```python theme={"dark"}
conn1 = Connection("127.0.0.1", 8080)
conn2 = Connection("192.168.1.1", 9000)


# Each instance maintains an independent namespace dictionary
print(conn1.__dict__)  # Output: {'host': '127.0.0.1', 'port': 8080}
print(conn2.__dict__)  # Output: {'host': '192.168.1.1', 'port': 9000}
```

## Dynamic Assignment and Deletion

Because Python objects are mutable by default, instance attributes are not strictly confined to the `__init__` method. They can be dynamically bound to or unbound from an instance at runtime using standard dot notation or the built-in `setattr()` and `delattr()` functions. Modifying the attributes dynamically directly mutates the instance's `__dict__`.

```python theme={"dark"}

# Dynamic assignment
conn1.timeout = 30
setattr(conn1, 'protocol', 'TCP')

print(conn1.__dict__) 

# Output: {'host': '127.0.0.1', 'port': 8080, 'timeout': 30, 'protocol': 'TCP'}


# Dynamic deletion
del conn1.port
delattr(conn1, 'host')

print(conn1.__dict__) 

# Output: {'timeout': 30, 'protocol': 'TCP'}
```

## Memory Optimization (`__slots__`)

By default, the use of `__dict__` incurs a memory overhead. If a class defines a `__slots__` iterable, Python suppresses the creation of the `__dict__` for instances of that class, allocating space only for a fixed set of instance attributes. This prevents the dynamic creation of new instance attributes outside of those explicitly declared.

```python theme={"dark"}
class OptimizedConnection:
    __slots__ = ['host', 'port']

    def __init__(self, host, port):
        self.host = host
        self.port = port

opt_conn = OptimizedConnection("10.0.0.1", 443)

# opt_conn.timeout = 30  # Raises AttributeError: 'OptimizedConnection' object has no attribute 'timeout'
```

<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>
