What does the following code demonstrate about Python’s name mangling? class Secure: def __init__(self): self.__secret = 42 s = Secure() print(s.__secret)

Python Professional Hard

Python Professional — Hard

What does the following code demonstrate about Python’s name mangling? class Secure: def __init__(self): self.__secret = 42 s = Secure() print(s.__secret)

Key points

  • Name mangling changes double underscore attributes to _ClassName__attribute
  • Mangled attributes are not directly accessible outside the class
  • Accessing __secret outside Secure class raises an AttributeError

Ready to go further?

Related questions