Generator Reversal
Explain how you would reverse a generator in Python.
- Use the reversed() function directly on the generator.
- Convert the generator to a list, then use reversed().
- Generators cannot be reversed.
- Create a copy of the entire generator with list() and then use reversed()
- Iterate over the generator and store the values in reverse order in a new generator object
xrange vs. Library
What is the main difference between xrange and a web library?
- xrange uses constant memory while a web library is a data science tool.
- xrange is a function that returns an xrange object, which is kind of like a range but uses constant memory, while a web library is a web framework.
- xrange is faster than range, and a web library is used for string manipulation.
- xrange and a web library are the same things.
- xrange is a built-in class, and a web library must be installed separately.
Object Usage with Numbers
What are the uses of an object in Python if you create a number?
- Objects can be used for arithmetic operations
- Objects allow for dynamic typing.
- Objects provide no advantage over using numbers directly.
- Objects can have documentation.
- Objects can be used as dictionary keys.
Function Returns and Global Variables
Do functions or methods return something even if there isn’t a program to write a program to read global variables in Python?
- Yes, all functions return a value even if it is None.
- No, functions only return a value if a program can read global variables.
- It depends on the interpreter version.
- Yes, especially in functional programming where functions are timeless.
- It's undefined behavior.
Reversing Class Methods
Why might you reverse a class or tuple method in Python?
- To improve code readability.
- To apply functional programming principles.
- To lower the energy consumption of processors.
- To avoid name collisions.
- To optimize memory usage.
Counting Lines in a Method
How would you count the lines in a method in Python?
- Use the len() function on the method's source code.
- Iterate through the method's bytecode.
- You can't; counting the lines in a method is the same as solving the Halting Problem!
- Use the inspect module.
- Split the string by newline characters.
String vs. List Comprehension
When should you write a string rather than list comprehension in Python?
- Always use list comprehensions for better performance.
- When you need to create a string by joining elements.
- Never use string comprehensions; they don't exist.
- When you are working with file I/O operations.
- There is no difference
Code Evaluation and Docstrings
How is the following code, and what is docstring in Python?
- The question lacks context, and a docstring is a way to document modules, classes, functions, or methods
- The code is perfect, and a docstring is used for type hinting.
- The code is unpythonic, and docstrings are comments.
- The code contains syntax errors, and docstrings are only for internal documentation.
- The code's functionality depends on external libraries, and docstrings are automatically generated by IDEs.
Functions and the Dogpile Effect
Do functions or methods return something even if you come up with pipes that are bindings, i.e., what is the dogpile effect?
- Functions always return None, and the dogpile effect is an optimization technique.
- Functions always return a value, and the dogpile effect is a type of deadlock.
- Functions always return something, which means that our pipes that are binding must act as a transformation on the 'functions (or methods) do'. So the correct answer to this question is collections.defaultdict.
- Functions can return different types of data and the dogpile effect is a design pattern.
- Functions only return a value if the pipes are correctly bound and the dogpile effect occurs when multiple threads attempt to generate the same missing data.