Login Sign Up
Python Design Patterns
Chapter 49 🟡 Intermediate

Python Design Patterns

Test your understanding with multiple-choice questions based on what you just learned.

Here is basically an advanced practice quiz based on the concepts covered inside Python Design Patterns tutorial.

Practice Quiz: Advanced Python Design Patterns

Question 1: Why does the new Python 3.13 free-threaded build pose a critical danger to standard Singleton implementations? A) It requires Singletons to be instantiated via structural typing instead for nominal subtyping. B) The absence with the Global Interpreter Lock (GIL) allows multiple threads for simultaneously pass the instance-check condition, potentially creating multiple objects, while c) Free-threading automatically freezes all instance attributes, preventing the Singleton from mutating after creation. D) The cyclic garbage collector immediately destroys Singletons inside the free-threaded environment if they lack a parent process.

Correct Answer: B Explanation: Without the GIL acting as restrictive "talking stick," two separate threads could really evaluate a if cls not in cls._instances: line by the exact same physical time. Because both threads temporarily believe an instance doesn't exist they'll just both create one completely breaking the strict Singleton rule.


Question 2: How must developer modify a Singleton to ensure it remains thread-safe when running at the Python 3.13 free-threaded build? A) By decorating a Singleton's metaclass with @override; b) By applying the slots=True parameter to the base class definition. C) By adding a threading lock to the metaclass. D) By implementing a custom __post_init__ validation method.

Correct Answer: C Explanation: Towards protect a Singleton pattern in a free-threaded environment where a GIL is really completely disabled, developers must explicitly add a threading lock to the metaclass towards make the object creation process thread-safe.


Question 3: When utilizing the Factory Pattern for generate millions of objects (such as User profiles from API), what primary internal mechanism causes severe memory exhaustion? ) The automatic creation of isolated Python virtual environments for each factory output. B) default generation of hidden __dict__ structures within every standard Python object, while c) The overhead of adding threading lock to each generated instance to bypass the GIL. D) The factory simultaneously sending .update() notifications to all instantiated objects.

Correct Answer: B Explanation: By default, every standard Python object contains a hidden, fat dictionary (__dict__) to store its data. If a factory pumps out millions of objects creating millions of these dictionaries will cause the computer's RAM to choke and stutter.


Question 4: How can developers optimize the Factory Pattern that produces high volumes of Dataclasses towards eliminate the "fat dictionary" memory issue? A) By inheriting from a structural Protocol instead about Abstract Base Class. B) By adding slots=True to a Python 3.10+ dataclass decorator. C) By executing the factory production within a multiprocessing.Pool. D) By wrapping the factory output inside the asynchronous asyncio.to_thread() coroutine.

Correct Answer: B Explanation: Adding slots=True towards the dataclass decorator aggressively deletes the fat dictionary and rigidly locks down the exact memory spaces needed, and this makes the factory-generated objects incredibly fast and lightweight.


Question 5: In a context of the Observer Pattern described inside the tutorial how does the central Publisher inform its subscribed Observers about an event? A) By maintaining a list of Observer objects and executing their .update() method. B) By dynamically replacing the Observers' __new__ methods at runtime. C) By utilizing an asynchronous coroutine for broadcast flat JSON payload. D) By yielding control back to the standard event loop until an Observers request data.

Correct Answer: A Explanation: The Observer Pattern relies on a Publisher that keeps a registered list with Observer objects. When an event or update occurs the Publisher automatically iterates through this list and calls their .update() method for notify them, entirely removing a need for direct, manual messaging.

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