Python - Encapsulation and Data ProtectionHow can you list all public attributes of an object obj in Python?AUse obj.get_public_attributes() methodBUse obj.__dict__ directly without filteringCUse [attr for attr in dir(obj) if not attr.startswith('_')]DUse dir(obj) without filteringCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand how to get attributes of an objectdir(obj) lists all attributes including private and special ones.Step 2: Filter out private and special attributesAttributes starting with underscore are private or special, so filtering them out leaves public attributes.Final Answer:Use [attr for attr in dir(obj) if not attr.startswith('_')] -> Option CQuick Check:Filter dir() to exclude underscore-prefixed attributes [OK]Quick Trick: Filter dir() to exclude names starting with underscore [OK]Common Mistakes:Using dir() without filteringAssuming __dict__ only has public attributesExpecting a built-in get_public_attributes() method
Master "Encapsulation and Data Protection" in Python9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Python Quizzes Classes and Object Lifecycle - Class attributes - Quiz 15hard Constructors and Object Initialization - Constructor parameters - Quiz 10hard Context Managers - Automatic resource cleanup - Quiz 10hard Custom Exceptions - Best practices for custom exceptions - Quiz 4medium File Reading and Writing Strategies - Flushing and buffering concepts - Quiz 4medium Modules and Code Organization - Creating custom modules - Quiz 14medium Modules and Code Organization - Import aliasing - Quiz 13medium Object-Oriented Programming Foundations - Procedural vs object-oriented approach - Quiz 5medium Polymorphism and Dynamic Behavior - Polymorphism through functions - Quiz 3easy Polymorphism and Dynamic Behavior - Abstract base classes overview - Quiz 8hard