PCPP1 practice question 10 of 10

Select the true statement related to PEP 257.

Full set: 45 questions
Answer options
A: String literals that occur immediately after another docstring are called attribute docstrings.
B: Attribute docstrings and Additional docstrings are two types of extra docstrings that can be extracted by software tools. Correct answer
C: String Iiterals that occur in places other than the first statement in a module, function, or class definition can act as documentation They are recognized by the Python bytecode compiler and are accessible as runtime object attributes
D: String literals that occur immediately after a simple assignment at the top level of a module are called complementary docstrings
Correct Answer:
B. Attribute docstrings and Additional docstrings are two types of extra docstrings that can be extracted by software tools.
Explanation

PEP 257, "Docstring Conventions," extends the concept of docstrings beyond the standard doc attribute attached to modules, functions, classes, and methods. It specifies two other types of docstrings that are not assigned to doc but can be processed by documentation tools. "Attribute docstrings" are string literals immediately following a public attribute assignment. "Additional docstrings" are string literals that appear immediately after another docstring. The PEP explicitly states that software tools may extract both of these for documentation purposes, making them recognized conventions.

Why Incorrect

A. String literals immediately after another docstring are called "additional docstrings," not "attribute docstrings." Attribute docstrings follow an attribute assignment.
C. Only a string literal that is the first statement in a module, function, or class is recognized by the compiler as a docstring and assigned to the doc attribute.
D. The term for a docstring following a top-level assignment is "attribute docstring." PEP 257 does not define or use the term "complementary docstrings."

References

1. Python Enhancement Proposals (PEP) 257: This document is the primary source for docstring conventions.

For Correct Answer B: The sections "Attribute Docstrings" and "Additional Docstrings" explicitly define these types and note: "Docstring processing tools may extract attribute docstrings..." and "These docstrings may be extracted by software tools." (Source: https://peps.python.org/pep-0257/)

For Incorrect Option A & D: The section "Attribute Docstrings" defines them as following an assignment, and the section "Additional Docstrings" defines them as following another docstring, clarifying the terminology. (Source: https://peps.python.org/pep-0257/)

2. The Python Language Reference, Section 8.7 "Function definitions": This official documentation confirms that a docstring must be the first statement in a definition body.

For Incorrect Option C: "The first statement of the function body can optionally be a string literal; this string literal is the function’s documentation string, or docstring." This clarifies that subsequent string literals are not treated as the primary docstring. (Source: https://docs.python.org/3/reference/compoundstmts.html#function-definitions)

3. MIT OpenCourseWare, 6.0001 Introduction to Computer Science and Programming in Python, Fall 2016, Lecture 5 Notes: University courseware reinforces the fundamental definition of a docstring.

For Incorrect Option C: The notes on "Specifications" for functions state, "In Python, we add a string as the first line of the function body" to serve as the docstring, confirming its required position. (Source: https://ocw.mit.edu/courses/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/resources/lec5functions/, Section: "Specifications")

Scroll to Top