Bird
0
0

How do you correctly declare a private attribute named __items inside the __init__ method of a Python class?

easy📝 Syntax Q3 of 15
Python - Encapsulation and Data Protection
How do you correctly declare a private attribute named __items inside the __init__ method of a Python class?
Aself.__items = []
B__items = []
Citems = []
Dprivate __items = []
Step-by-Step Solution
Solution:
  1. Step 1: Use self to define instance attributes

    Instance attributes must be prefixed with self. inside methods.
  2. Step 2: Use double underscore for private attribute

    Prefixing with double underscore makes it private via name mangling.
  3. Final Answer:

    self.__items = [] -> Option A
  4. Quick Check:

    Instance attributes need self. prefix [OK]
Quick Trick: Private attributes need self.__ prefix [OK]
Common Mistakes:
  • Omitting self. when defining attributes
  • Using invalid syntax like 'private' keyword
  • Defining attribute without underscores

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes