186k views
5 votes
Class Example:

def __init__(self):
self._value = True

def change_value(self):
self._value = False

# Instantiate the Example class
quiz = Example
quiz._value = False

Which of the following sentences are true (several answers are correct)?

1. quiz is not an instance of Example (parentheses are missing)
2. "Example" is not a valid class name for PEP8
3. Indentation is incorrect for the "change_value" method
4. The program does not respect the encapsulation principle

1 Answer

3 votes

Final answer:

quiz is an instance of Example without parentheses since they are optional; Example is a valid class name following PEP 8 guidelines; while the direct access of _value breaches the encapsulation principle.

Step-by-step explanation:

The student has asked about the correctness of certain statements given a code snippet for a Python class named Example. Let's evaluate each statement in turn:

quiz is indeed an instance of Example, even without using parentheses. In Python, parentheses are optional if the class does not explicitly require arguments for its constructor.

"Example" is a valid class name according to PEP 8 guidelines which recommend CamelCase notation for class names.

The indentation of the "change_value" method in the question appears incorrect based on common Python formatting standards where each level is indented by 4 spaces or a tab, but without the full context, we cannot assert this with certainty.

Encapsulation is a principle that suggests members of a class should not be accessed directly but through methods provided. The indicated operation, quiz._value = False, does not respect this principle as it accesses a protected member (prefixed with an underscore) directly from outside the class definition.

User Eaten By A Grue
by
8.2k points