Complete the code to ensure only one instance of the class is created (Singleton pattern).
class Singleton: _instance = None def __new__(cls): if cls._instance is [1]: cls._instance = super().__new__(cls) return cls._instance
The Singleton pattern ensures only one instance exists. Checking if _instance is None means no instance has been created yet.
Complete the Factory method to create a product based on the type.
class Factory: def create_product(self, product_type): if product_type == 'A': return ProductA() elif product_type == 'B': return [1]() else: return None
The Factory method returns an instance of the requested product type. For 'B', it should return ProductB().
Fix the error in the Builder pattern's build method to return the constructed object.
class Builder: def __init__(self): self.product = Product() def build(self): self.product.add_part('Part1') self.product.add_part('Part2') return [1]
The build method should return the constructed product object, which is stored in self.product.
Fill both blanks to complete the Singleton pattern's thread-safe instance retrieval.
import threading class Singleton: _instance = None _lock = threading.Lock() @classmethod def get_instance(cls): with cls.[1]: if cls._instance is [2]: cls._instance = cls() return cls._instance
To make Singleton thread-safe, a lock is used. The lock attribute is named lock. The instance is checked against None to see if it needs creation.
Fill all three blanks to complete a dictionary comprehension that builds a product catalog with prices above zero.
products = {'apple': 1.2, 'banana': 0, 'cherry': 2.5}
catalog = [1]: [2] for [3] in products.items() if [2] > 0The dictionary comprehension creates a new dictionary with product names as keys and prices as values, but only includes products with price greater than zero.