Complete the code to access the mangled private variable inside the class.
class MyClass: def __init__(self): self.__secret = 42 obj = MyClass() print(obj.[1])
In Python, variables with double underscores are name-mangled to include the class name. So __secret becomes _MyClass__secret.
Complete the code to define a method that accesses the mangled private variable.
class SecretHolder: def __init__(self): self.__hidden = 'safe' def reveal(self): return self.[1]
The method must use the mangled name _SecretHolder__hidden to access the private variable.
Fix the error in accessing the private variable from outside the class.
class Data: def __init__(self): self.__value = 100 obj = Data() print(obj.[1])
Outside the class, the private variable must be accessed using the mangled name _Data__value.
Fill both blanks to create a dictionary comprehension that maps attribute names to their mangled values.
class Info: def __init__(self): self.__a = 1 self.__b = 2 obj = Info() attrs = {name: getattr(obj, [1]) for name in ['a', 'b'] if hasattr(obj, [2])}
The mangled names are formed by prefixing the class name and double underscore to the attribute name, like _Info__a.
Fill all three blanks to define a class with a private variable, a method to access it, and external access using name mangling.
class Secret: def __init__(self, val): self.[1] = val def get_secret(self): return self.[2] s = Secret('hidden') print(s.[3])
The private variable is named __data. Inside the class, it is accessed as _Secret__data due to name mangling. Outside, the mangled name _Secret__data is used to access it.