Complete the code to define a class with a private attribute.
class Person: def __init__(self, name): self.[1]name = name
In Python, prefixing an attribute with double underscores __ makes it private (name mangling).
Complete the method to access the private attribute safely.
class Person: def __init__(self, name): self.__name = name def get_name(self): return self.[1]name
Private attributes are name-mangled in Python. To access __name outside, use _ClassName__name.
Fix the error in the setter method to update the private attribute.
class Person: def __init__(self, name): self.__name = name def set_name(self, name): self.[1]name = name
The setter must update the private attribute using the exact private name __name with double underscores.
Fill both blanks to create a dictionary comprehension that stores private attribute lengths for names longer than 3.
class Person: def __init__(self, name): self.__name = name names = [Person('Anna'), Person('Bob'), Person('Catherine')] lengths = {person.[1]name: len(person.[2]name) for person in names if len(person._Person__name) > 3}
To access the private attribute outside the class, use the name-mangled form _Person__name.
Fill all three blanks to create a method that safely updates the private attribute only if the new name is longer than 2 characters.
class Person: def __init__(self, name): self.__name = name def update_name(self, new_name): if len(new_name) > [1]: self.[2]name = new_name return True else: return [3]
The method checks if the new name length is greater than 2, updates the private attribute __name, and returns False otherwise.