Certified Professional in Python Programming (PCPP-32-101) — Question 33
Select the true statements related to PEP 8 programming recommendations for code writing. (Choose two.)
Answer options
- A. You should use the not ... is operator (e.g. if not spam is None:), rather than the is not operator (e.g. if spam is not None:), to increase readability.
- B. You should make object type comparisons using the isinstance() method (e.g. if isinstance(obj , int):) instead of comparing types directly (e.g. if type(obj) is type(1)).
- C. You should write code in a way that favors the CPython implementation over PyPy, Cython, and Jython.
- D. You should not write string literals that rely on significant trailing whitespaces, as they may be visually indistinguishable, and certain editors may trim them.
Correct answer: B, D
Explanation
Option B is correct because PEP 8 recommends using isinstance() for type checking, which is more robust and supports inheritance. Option D is also correct as it highlights the issues with trailing whitespace in string literals, which can cause confusion in code readability. Options A and C are incorrect; A suggests a less readable practice and C contradicts the intention of writing portable code.