Functions and Modularity Flashcards
7 cards from real HACKERRANK practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Functions and Modularity flashcards as text
What does `inspect.signature(func)` return?
Answer: A Signature object describing the function's parameters
`inspect.signature` returns a `Signature` object that contains information about a function's parameters and annotations.
Which of the following correctly creates a package in Python?
Answer: A directory containing a file named `__init__.py`
A Python package is a directory that contains an `__init__.py` file, which marks it as a package to the interpreter.
What is the output? ```python def f(*args, **kwargs): return len(args), len(kwargs) print(f(1, 2, x=3, y=4)) ```
Answer: (2, 2)
Positional arguments 1 and 2 go into `args` (length 2), and keyword arguments x and y go into `kwargs` (length 2).
What is the purpose of a decorator in Python?
Answer: To wrap a function and modify or extend its behavior
A decorator is a callable that takes a function and returns a modified version, allowing behavior to be added before or after the original function.
Which keyword allows an inner function to reassign a variable defined in an enclosing (non-global) scope?
Answer: nonlocal
`nonlocal` explicitly declares that a variable belongs to the nearest enclosing scope, allowing reassignment inside inner functions.
What does `help(module)` display in Python?
Answer: The module's documentation including docstrings of its contents
`help()` uses the `pydoc` system to display a module's docstring and the docstrings of its functions, classes, and methods.
What is the difference between `import module` and `from module import *`?
Answer: `import module` keeps names in the module's namespace; `from module import *` dumps all public names into the current namespace
`import module` requires `module.name` access, while `from module import *` pulls all public names directly into the current namespace, risking name collisions.