Complete the code to declare an immutable data structure.
class Config: def __init__(self, settings): self._settings = [1](settings) def get(self, key): return key in self._settings
Using frozenset makes the settings immutable, preventing accidental changes.
Complete the code to ensure thread-safe access to shared data using immutability.
class SharedData: def __init__(self, data): self._data = [1](data) def read(self): return self._data
Using a tuple ensures the data cannot be changed, making it safe for concurrent reads.
Fix the error in the code that tries to modify an immutable object.
config = [1](['read', 'write']) config.append('execute')
Only list supports the append method; tuples and frozensets are immutable.
Fill both blanks to create an immutable configuration dictionary.
from types import [1] config = [2]({'host': 'localhost', 'port': 8080})
MappingProxyType creates a read-only view of a dictionary, making it immutable.
Fill all three blanks to implement a safe update method that returns a new immutable config.
def update_config(old_config, key, value): new_dict = dict(old_config) new_dict[[1]] = [2] return [3](new_dict)
The function copies the old config, updates the key with the new value, and returns an immutable proxy.