PCPP1 practice question 9 of 10
Select the true statements related to PEP 8 naming conventions. (Select two answers.)
Full set: 45 questionsPEP 8, the official style guide for Python code, provides explicit naming conventions to enhance code readability and consistency. It specifies that function and variable names should use snakecase, which is lowercase with words separated by underscores. This convention distinguishes them from CamelCase, which is reserved for class names. Furthermore, PEP 8 strongly recommends using self as the name for the first argument of an instance method and cls for the first argument of a class method. While these are not enforced by the interpreter, they are a near-universal convention followed by the Python community to maintain clarity.
C. This is incorrect. PEP 8 states that modules should have short, all-lowercase names, and underscores can be used if it improves readability. CamelCase is for class names.
D. This is incorrect. PEP 8 specifies that constants should be written in all capital letters with words separated by underscores (e.g., MAXCONNECTIONS).
1. PEP 8 -- Style Guide for Python Code. (2001, updated 2013). G. van Rossum, B. Warsaw, N. Coghlan. Python Software Foundation.
For Answer A (Function and Variable Names): In the "Naming Conventions" section, under "Function and Variable Names," it states: "Function names should be lowercase, with words separated by underscores as necessary to improve readability. Variable names follow the same convention as function names."
For Answer B (Method Arguments): In the "Naming Conventions" section, under "Method Names and Instance Variables," it states: "Always use self for the first argument to instance methods. Always use cls for the first argument to class methods."
For Answer C (Module Names): In the "Naming Conventions" section, under "Package and Module Names," it states: "Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability."
For Answer D (Constants): In the "Naming Conventions" section, under "Constants," it states: "Constants are usually defined on a module level and written in all capital letters with underscores separating words."
2. University of California, Berkeley, CS 61A Course Materials. (Fall 2023). "Style and Composition." The course style guide, which is based on PEP 8, reinforces these conventions for students. It explicitly mentions using snakecase for functions and variables and UPPERSNAKECASE for constants. (Referenced in the course's general style guide documents).
Keep practicing
Take the free PCPP1 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other PCPP1 practice questions
- Question 1Select the true statements about the connection-oriented and connectionless types of comm…
- Question 2Select the true statements about the sqlite3 module. (Select two answers.)
- Question 4Select the true statements about the sqirte3 module. (Select two answers.)
- Question 5If w is a correctly created main application window, which method would you use to foe bo…
- Question 7Select the true statements about the json.-dumps () function. (Select two answers.)
- Question 10Select the true statement related to PEP 257.
