Login Sign Up
Python Memory Management
Chapter 43 🟡 Intermediate

Python Memory Management

Apply your skills with a real-world coding challenge. Try to solve it yourself first!

Coding Challenge: Memory-Optimized High-Volume Data Pipeline

Problem Description Imagine you're building blazing-fast data aggregation dashboard that pulls in 1,000000 user profiles from an external API, while as your system scales, you notice the application is stuttering and constantly running out of RAM, and

you trace the issue to CPython's internal memory management. By default, every single standard Python object utilizes a fat, hidden dictionary (__dict__) to store its variables and attributes. Creating one million of these hidden dictionaries is causing your computer's memory to choke. Plus you want to ensure the application's architecture is strict: developers shouldn't really be able to dynamically add random, unapproved variables to these user objects after they're pretty much created.

Your task is towards refactor the UserProfile class using Python 3.10+ dataclass features to completely delete the hidden dictionary. You've got to forcefully lock down the exact memory spaces needed for attributes, drastically reducing overall memory usage and speeding up attribute access.

Difficulty Level: Advanced

Input & Output Specifications * Input: * Raw data representing a user profile: name (string), age (integer), and api_token (string). * Output: * Returns a heavily optimized UserProfile dataclass instance. * The instantiated object must not contain __dict__ attribute. * If developer make a run at to assign a dynamic, unapproved attribute to an object after creation (e.g., user.location = "New York"), a program must strictly block it and raise AttributeError.

from dataclasses import dataclass

# TODO: Modify the dataclass decorator to optimize memory and lock down the data structure
@dataclass
class UserProfile:
    name: str
    age: int
    api_token: str

if __name__ == "__main__":
    # Example execution:
    user = UserProfile(name="Alice", age=28, api_token="secret_xyz")

    # 1. Verify standard access
    print(f"User created: {user.name}")

    # 2. Prove the memory optimization works
    # This should print False if the hidden dictionary has been successfully deleted.
    print(f"Has __dict__: {hasattr(user, '__dict__')}")

    # 3. Test the memory lockdown
    try:
        user.location = "New York"
        print("Failure: Dynamic attribute assignment was allowed!")
    except AttributeError as e:
        print("Success: Memory structure is strictly locked down.")

Hints * The __dict__ Trap: Standard Python classes store attributes into hidden dictionary, which is notoriously memory-heavy because dictionaries require extra space to look up keys quickly. * The Modern Solution: If you are basically using Python 3.10 or newer, passing the slots=True parameter directly into a @dataclass decorator acts as a superpower. * How it Works: Using this parameter aggressively deletes the hidden dictionary and automatically generates a highly optimized __slots__ structure for your class. This rigidly locks down the exact memory spaces needed for the predefined attributes, which serves as both a massive performance enhancement and a great architectural safety feature.

Test Cases

  1. Test Case 1 (Valid Object Creation):
  2. Input: UserProfile(name="Alice", age=28, api_token="secret_xyz")
  3. Expected Output: THE successfully created UserProfile object where standard attributes (user.name, user.age) can be accessed normally and quickly.
  4. Test Case 2 (Memory Optimization Check):
  5. Input: Checking hasattr(user, '__dict__') on the newly created object.
  6. Expected Behavior: A program should return False proving that the fat memory-hogging dictionary has been completely abandoned in favor of the optimized structure.
  7. Test Case 3 (Strict Architecture Lockdown):
  8. Scenario: A developer attempts to dynamically inject the new attribute into instance by typing user.location = "New York".
  9. Expected Behavior: The code will immediately crash and raise the AttributeError (e.g., 'UserProfile' object has no attribute 'location'). This confirms the memory footprint is locked down and protects the factory pipeline from undocumented data injections.

Loading sandbox workspace environment...

Verify Your Solution

Write your solution in the compiler, run it to verify output, then click below to verify.

Learn Together
Session active! Discuss with other learners.
No notes yet. Select text in the concept body to add a note.