Which THREE features are associated with robust logging in production applications?
Prevents logs from growing indefinitely.
Why this answer
Logging levels, rotation, and external log aggregation are standard.
35 questions · Best Practices And Coding Conventions topic · All types, answers revealed
Which THREE features are associated with robust logging in production applications?
Prevents logs from growing indefinitely.
Why this answer
Logging levels, rotation, and external log aggregation are standard.
In a team environment, which PEP 8 guideline should be applied to maintain consistent indentation when mixing spaces and tabs?
Spaces are the preferred indentation method according to PEP 8.
Why this answer
PEP 8 states that spaces are the preferred indentation method over tabs to ensure cross-platform consistency.
Which TWO of these are valid PEP 8 recommendations for whitespace?
Trailing whitespace is unnecessary clutter.
Why this answer
Avoid trailing whitespace and use spaces around assignments only if necessary.
You are implementing a custom context manager using a class. Which methods must be implemented to support the 'with' statement?
These are the mandatory methods for the Context Manager protocol.
Why this answer
A context manager requires `__enter__` to set up the resource and `__exit__` to handle cleanup.
When creating a new project, how should you structure your imports?
This is the order specified in PEP 8.
Why this answer
PEP 8 states that imports should be grouped into standard library imports, third-party imports, and local application imports.
Which TWO practices help maintain clean code when using Python's 'try-except' blocks?
Reduces accidental catching of unrelated errors.
Why this answer
Keep the try block small and catch specific exceptions.
What is the recommended way to handle a function that requires a large number of arguments?
This improves readability and maintainability.
Why this answer
If a function has too many arguments, it is often a sign it should be broken down or that a configuration object should be used.
When should you use absolute imports over relative imports in a large project?
Absolute imports are less prone to issues with package refactoring.
Why this answer
Absolute imports are recommended by PEP 8 because they are clearer and avoid ambiguity in complex package structures.
According to PEP 257, what is the standard format for a single-line docstring?
This is the recommended convention for short docstrings.
Why this answer
A single-line docstring should be on the same line as the opening triple quotes, ending with a period.
Which TWO practices ensure secure handling of sensitive environment configurations?
Prevents accidental commits.
Why this answer
Using .env files and ignoring them in version control is the standard.
A script processes data from untrusted sources. Which technique best mitigates 'pickle' deserialization vulnerabilities?
These formats do not support object reconstruction via arbitrary code execution.
Why this answer
The 'pickle' module is fundamentally insecure; using secure formats like JSON is the only way to avoid code injection during deserialization.
When naming a constant in Python, which convention does PEP 8 recommend?
The standard convention for constants.
Why this answer
Constants should be named using all capital letters with underscores to separate words.
Which THREE of the following are recommended in PEP 8 for class definitions?
Standard spacing.
Why this answer
Use CamelCase, omit blank lines before classes, and use docstrings.
You need to ensure your code is readable by other Python developers. Which naming style should be used for variable names?
The standard Python convention for variables.
Why this answer
PEP 8 prescribes snake_case (lowercase with underscores) for variable and function names.
Which THREE practices are recommended when handling external libraries?
This keeps dependencies isolated.
Why this answer
You should use virtual environments, pin your dependencies, and document your requirements.
When is it acceptable to ignore PEP 8 guidelines?
Consistency is a key tenet of the style guide.
Why this answer
PEP 8 itself states that project consistency and backward compatibility may warrant exceptions to the rules.
When designing a public API, how should you signal that a method is for internal use only?
This is the established convention in Python.
Why this answer
Prefixing a name with a single underscore is the conventional way to indicate internal use.
When writing a library, what is the best practice for documenting the usage of a function?
Docstrings are the built-in standard for Python documentation.
Why this answer
Docstrings provide the most standard and accessible form of documentation that can be integrated with tools like Sphinx.
In the context of writing robust error handling, what is the 'EAFP' principle?
This is the fundamental philosophy behind Python's exception handling.
Why this answer
EAFP stands for 'Easier to Ask for Forgiveness than Permission', which encourages using try-except blocks instead of pre-checking states.
You are refactoring code that performs sensitive operations. Which of the following is the most secure practice for handling secret keys?
Environment variables keep secrets out of the source code repository.
Why this answer
Hardcoding secrets is a major security vulnerability; using environment variables is the industry standard.
When implementing secure coding practices in a web-facing Python application, how should you handle raw user input to prevent command injection?
This approach treats the input as data rather than an executable command string.
Why this answer
Using the 'subprocess' module with 'shell=False' (the default) prevents the shell from interpreting user-provided strings as commands.
You are writing a library that expects a custom exception. Which practice aligns best with Python's exception handling hierarchy?
Exception is the correct base class for user-defined exceptions.
Why this answer
Custom exceptions should inherit from the built-in Exception class to ensure they are catchable by standard error handlers.
Which TWO of these identify a potential 'code smell' in Python exception handling?
Silently failing makes debugging nearly impossible.
Why this answer
Swallowing exceptions and catching BaseException are both poor practices.
Which of the following is the correct way to handle whitespace around an operator according to PEP 8?
This follows the PEP 8 spacing rule.
Why this answer
PEP 8 dictates surrounding operators with a single space on both sides.
How should one handle the 'KeyboardInterrupt' exception if it must be caught for cleanup purposes?
This pattern allows for cleanup while respecting the user's intent to exit.
Why this answer
You should catch it, perform the cleanup, and then re-raise it so the application still shuts down gracefully.
When documenting a complex function, which approach is most compliant with Google Style Python Docstrings regarding parameter types?
This is the standard format for Google-style docstrings.
Why this answer
Google Style requires explicit listing of types in the Args section to improve readability for static analysis tools.
What is the primary benefit of using 'logging' over 'print' statements for application diagnostics?
This is the core advantage of a structured logging system.
Why this answer
The 'logging' module provides granular control over levels, destinations, and output formatting without changing the source code.
Which THREE elements are essential for a good Python docstring?
Helps clarify complex behavior.
Why this answer
A summary line, a blank line, and a detailed description are standard.
According to PEP 8, what is the maximum recommended length for a line of code?
This is the exact PEP 8 recommendation.
Why this answer
PEP 8 suggests limiting lines to 79 characters to allow multiple files to be opened side-by-side.
Which comment style is preferred for block comments in Python?
This is the PEP 8 standard for block comments.
Why this answer
Block comments should consist of paragraphs of text, each starting with a # and a single space.
Which TWO of the following are valid conventions for naming in Python according to PEP 8?
Correct convention for classes.
Why this answer
Functions should use snake_case, and classes should use PascalCase.
Which THREE practices improve documentation maintenance?
Keeps info accurate.
Why this answer
Using standard formats, using tools, and regular updates are key.
Which practice is recommended when dealing with 'bare' except clauses in Python?
This prevents catching unintended system-level exceptions.
Why this answer
Bare 'except:' clauses catch SystemExit and KeyboardInterrupt, which can prevent the user from stopping a program.
Which TWO of the following are secure coding practices in Python?
Pickle is inherently insecure.
Why this answer
Avoiding dangerous functions and validating all inputs are key security practices.
You are debugging a legacy application and need to ensure that resources like file handles are always closed. Which construct is preferred?
This is the Pythonic way to handle resource management.
Why this answer
The 'with' statement (context manager) ensures that cleanup code is executed even if an exception occurs.
Ready to test yourself?
Try a timed practice session using only Best Practices And Coding Conventions questions.