LearnFrenzy
Sign in Take a free test

OOP in Python · Predict the Output · Question 8

Name mangling Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

What is the output of the following Python program?

main.py
class A:
    def __init__(self):
        self.__secret = 1
        self._hint = 2

a = A()
print(hasattr(a, "__secret"), a._A__secret, a._hint, sorted(vars(a)))
  • A True 1 2 ['__secret', '_hint']
  • B False 1 2 ['_A__secret', '_hint'] Correct
  • C False 1 2 ['__secret', '_hint']
  • D AttributeError
Correct Answer: Option B

__secret is stored as _A__secret; a single underscore name is stored as written.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links